Imported Upstream version 0.63.0
[hcoop/debian/courier-authlib.git] / authldaplib.c
index b001278..26ba8ac 100644 (file)
@@ -391,6 +391,7 @@ int l=strlen(env);
                for (i=0; i<ldapauth_size; i++)
                        if (ldapauth[i] == '\n')
                                ldapauth[i]=0;
+               fclose(f);
        }
 
        for (i=0; i<ldapauth_size; )
@@ -1073,8 +1074,6 @@ static int auth_ldap_do2(const char *service,
                        int (*callback)(struct authinfo *, void *),
                         void *arg, const char *newpass);
 
-static char *escape_str(const char *);
-
 static int auth_ldap_retry(const char *service,
                           const char *user, const char *pass,
                           int (*callback)(struct authinfo *, void *),
@@ -1102,11 +1101,13 @@ static int auth_ldap_retry(const char *service,
        char *q;
        int i;
 
-       q=escape_str(user);
+       q=courier_auth_ldap_escape(user);
 
        if (!q)
-               return (auth_ldap_do2(service,
-                                     user, pass, callback, arg, newpass));
+       {
+               perror("malloc");
+               return 1;
+       }
 
        i=auth_ldap_do2(service, q, pass, callback, arg, newpass);
        free(q);
@@ -1179,11 +1180,10 @@ static int auth_ldap_do2(const char *service,
 
        if ((cnt=ldap_count_entries(my_ldap_fp, result)) != 1)
        {
-               free(srch);
-
                if (cnt)
                        err("emailmap: %d entries returned from search %s (but we need exactly 1)",
                               cnt, srch);
+               free(srch);
                ldap_msgfree(result);
                return -1;
        }
@@ -1736,43 +1736,6 @@ static int auth_ldap_do3(const char *service,
        return (rc);
 }
 
-/*
-** Escape a string with special LDAP characters.  Returns NULL if the original
-** string does not have any special LDAP characters (so we don't allocate
-** memory unless absolutely necessary).
-*/
-
-static char *escape_str(const char *user)
-{
-       int i;
-       const char *p;
-       char *q, *r;
-
-       for (i=0, p=user; *p; p++)
-               if (strchr("*()\\", *p))
-                       ++i;
-
-       if (i == 0)
-               return NULL;    /* No need to escape anything */
-
-       q=malloc(strlen(user)+i+1);
-
-       if (!q)
-       {
-               perror("malloc");
-               exit(1);
-       }
-
-       for (r=q, p=user; *p; p++)
-       {
-               if (strchr("*()\\", *p))
-                       *r++= '\\';
-               *r++ = *p;
-       }
-       *r=0;
-       return (q);
-}
-
 /**
  ** Create an emailmap search string.  I'm going to wrap this into an external
  ** variable, so I'll use generic coding here.
@@ -2035,6 +1998,7 @@ void auth_ldap_enumerate( void(*cb_func)(const char *name,
 
                                if (!options)
                                {
+                                       l_value_free(names);
                                        perror("CRIT: auth_ldap_enumerate: malloc failed");
                                        return;
                                }