]> git.sur5r.net Git - openldap/blobdiff - libraries/librewrite/ldapmap.c
Happy new year!
[openldap] / libraries / librewrite / ldapmap.c
index 07e04bccf2d831e99340f9b2e11bab2d3ca54851..186fd601dc5f62e723edb73f6184ccac0b7f1eba 100644 (file)
@@ -1,29 +1,25 @@
-/******************************************************************************
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
+ * Copyright 2000-2005 The OpenLDAP Foundation.
  * All rights reserved.
  *
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
  *
- * 1. The author is not responsible for the consequences of use of this
- * software, no matter how awful, even if they arise from flaws in it.
- *
- * 2. The origin of this software must not be misrepresented, either by
- * explicit claim or by omission.  Since few users ever read sources,
- * credits should appear in the documentation.
- *
- * 3. Altered versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.  Since few users
- * ever read sources, credits should appear in the documentation.
- * 
- * 4. This notice may not be removed or altered.
- *
- ******************************************************************************/
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENT:
+ * This work was initially developed by Pierangelo Masarati for
+ * inclusion in OpenLDAP Software.
+ */
 
 #include <portable.h>
 
+#define LDAP_DEPRECATED 1
 #include "rewrite-int.h"
 #include "rewrite-map.h"
 
  * LDAP map data structure
  */
 struct ldap_map_data {
-       char                           *url;
-       LDAPURLDesc                    *lud;
-       int                             attrsonly;
-       char                           *binddn;
-       char                           *bindpw;
+       char                           *lm_url;
+       LDAPURLDesc                    *lm_lud;
+       int                             lm_attrsonly;
+       char                           *lm_binddn;
+       char                           *lm_bindpw;
 
 #define MAP_LDAP_EVERYTIME             0x00
 #define MAP_LDAP_NOW                   0x01
 #define MAP_LDAP_LATER                 0x02
-       int                             when;
+       int                             lm_when;
 
-       LDAP                           *ld;
+       LDAP                           *lm_ld;
 
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
-       ldap_pvt_thread_mutex_t         mutex;
+       ldap_pvt_thread_mutex_t         lm_mutex;
 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
 };
 
@@ -56,24 +52,24 @@ map_ldap_free(
 {
        assert( data != NULL );
 
-       if ( data->url != NULL ) {
-               free( data->url );
+       if ( data->lm_url != NULL ) {
+               free( data->lm_url );
        }
 
-       if ( data->lud != NULL ) {
-               ldap_free_urldesc( data->lud );
+       if ( data->lm_lud != NULL ) {
+               ldap_free_urldesc( data->lm_lud );
        }
 
-       if ( data->binddn != NULL ) {
-               free( data->binddn );
+       if ( data->lm_binddn != NULL ) {
+               free( data->lm_binddn );
        }
 
-       if ( data->bindpw != NULL ) {
-               free( data->bindpw );
+       if ( data->lm_bindpw != NULL ) {
+               free( data->lm_bindpw );
        }
 
-       if ( data->when != MAP_LDAP_EVERYTIME && data->ld != NULL ) {
-               ldap_unbind_s( data->ld );
+       if ( data->lm_when != MAP_LDAP_EVERYTIME && data->lm_ld != NULL ) {
+               ldap_unbind_s( data->lm_ld );
        }
 
        free( data );
@@ -108,13 +104,13 @@ map_ldap_parse(
                return NULL;
        }
 
-       data->url = strdup( argv[ 0 ] );
-       if ( data->url == NULL ) {
+       data->lm_url = strdup( argv[ 0 ] );
+       if ( data->lm_url == NULL ) {
                map_ldap_free( data );
                return NULL;
        }
        
-       if ( ldap_url_parse( argv[ 0 ], &data->lud ) != REWRITE_SUCCESS ) {
+       if ( ldap_url_parse( argv[ 0 ], &data->lm_lud ) != REWRITE_SUCCESS ) {
                Debug( LDAP_DEBUG_ANY,
                                "[%s:%d] illegal URI '%s'\n",
                                fname, lineno, argv[ 0 ] );
@@ -122,14 +118,14 @@ map_ldap_parse(
                return NULL;
        }
 
-       p = strchr( data->url, '/' );
+       p = strchr( data->lm_url, '/' );
        assert( p[ 1 ] == '/' );
        if ( ( p = strchr( p + 2, '/' ) ) != NULL ) {
                p[ 0 ] = '\0';
        }
 
-       if ( strcasecmp( data->lud->lud_attrs[ 0 ], "dn" ) == 0 ) {
-               data->attrsonly = 1;
+       if ( strcasecmp( data->lm_lud->lud_attrs[ 0 ], "dn" ) == 0 ) {
+               data->lm_attrsonly = 1;
        }
              
        for ( argc--, argv++; argc > 0; argc--, argv++ ) {
@@ -148,19 +144,19 @@ map_ldap_parse(
                                l = strlen( p );
                        }
                        
-                       data->binddn = strdup( p );                     
-                       if ( data->binddn == NULL ) {
+                       data->lm_binddn = strdup( p );                  
+                       if ( data->lm_binddn == NULL ) {
                                map_ldap_free( data );
                                return NULL;
                        }
 
-                       if ( data->binddn[ l ] == '\"' 
-                                       || data->binddn[ l ] == '\'' ) {
-                               data->binddn[ l ] = '\0';
+                       if ( data->lm_binddn[ l ] == '\"' 
+                                       || data->lm_binddn[ l ] == '\'' ) {
+                               data->lm_binddn[ l ] = '\0';
                        }
                } else if ( strncasecmp( argv[ 0 ], "bindpw=", 7 ) == 0 ) {
-                       data->bindpw = strdup( argv[ 2 ] + 7 );
-                       if ( data->bindpw == NULL ) {
+                       data->lm_bindpw = strdup( argv[ 2 ] + 7 );
+                       if ( data->lm_bindpw == NULL ) {
                                map_ldap_free( data );
                                return NULL;
                        }
@@ -170,30 +166,30 @@ map_ldap_parse(
                        if ( strcasecmp( p, "now" ) == 0 ) {
                                int rc;
                                
-                               data->when = MAP_LDAP_NOW;
+                               data->lm_when = MAP_LDAP_NOW;
                                
                                /*
                                 * Init LDAP handler ...
                                 */
-                               rc = ldap_initialize( &data->ld, data->url );
+                               rc = ldap_initialize( &data->lm_ld, data->lm_url );
                                if ( rc != LDAP_SUCCESS ) {
                                        map_ldap_free( data );
                                        return NULL;
                                }
 
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
-                               ldap_pvt_thread_mutex_init( &data->mutex );
+                               ldap_pvt_thread_mutex_init( &data->lm_mutex );
 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
 
                        } else if ( strcasecmp( p, "later" ) == 0 ) {
-                               data->when = MAP_LDAP_LATER;
+                               data->lm_when = MAP_LDAP_LATER;
 
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
-                               ldap_pvt_thread_mutex_init( &data->mutex );
+                               ldap_pvt_thread_mutex_init( &data->lm_mutex );
 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
 
                        } else if ( strcasecmp( p, "everytime" ) == 0 ) {
-                               data->when = MAP_LDAP_EVERYTIME;
+                               data->lm_when = MAP_LDAP_EVERYTIME;
                        } else {
                                /* ignore ... */
                        }
@@ -216,7 +212,7 @@ map_ldap_apply(
        char **values;
        int rc;
        struct ldap_map_data *data = ( struct ldap_map_data * )map->lb_private;
-       LDAPURLDesc *lud = data->lud;
+       LDAPURLDesc *lud = data->lm_lud;
        
        int first_try = 1;
 
@@ -229,20 +225,21 @@ map_ldap_apply(
        val->bv_val = NULL;
        val->bv_len = 0;
 
-       if ( data->when == MAP_LDAP_EVERYTIME ) {
-               rc = ldap_initialize( &ld, data->url );
+       if ( data->lm_when == MAP_LDAP_EVERYTIME ) {
+               rc = ldap_initialize( &ld, data->lm_url );
+
        } else {
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
-               ldap_pvt_thread_mutex_lock( &data->mutex );
+               ldap_pvt_thread_mutex_lock( &data->lm_mutex );
 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
 
                rc = LDAP_SUCCESS;
 
-               if ( data->when == MAP_LDAP_LATER && data->ld == NULL ) {
-                       rc = ldap_initialize( &data->ld, data->url );
+               if ( data->lm_when == MAP_LDAP_LATER && data->lm_ld == NULL ) {
+                       rc = ldap_initialize( &data->lm_ld, data->lm_url );
                }
                
-               ld = data->ld;
+               ld = data->lm_ld;
        }
 
        if ( rc != LDAP_SUCCESS ) {
@@ -251,15 +248,16 @@ map_ldap_apply(
        }
 
 do_bind:;
-       if ( data->binddn != NULL ) {
-               rc = ldap_simple_bind_s( ld, data->binddn, data->bindpw );
+       if ( data->lm_binddn != NULL ) {
+               rc = ldap_simple_bind_s( ld, data->lm_binddn, data->lm_bindpw );
                if ( rc == LDAP_SERVER_DOWN && first_try ) {
                        first_try = 0;
-                       if ( ldap_initialize( &ld, data->url ) != LDAP_SUCCESS ) {
+                       if ( ldap_initialize( &ld, data->lm_url ) != LDAP_SUCCESS ) {
                                rc = REWRITE_ERR;
                                goto rc_return;
                        }
                        goto do_bind;
+
                } else if ( rc != REWRITE_SUCCESS ) {
                        rc = REWRITE_ERR;
                        goto rc_return;
@@ -267,14 +265,15 @@ do_bind:;
        }
 
        rc = ldap_search_s( ld, lud->lud_dn, lud->lud_scope, ( char * )filter,
-                       lud->lud_attrs, data->attrsonly, &res );
+                       lud->lud_attrs, data->lm_attrsonly, &res );
        if ( rc == LDAP_SERVER_DOWN && first_try ) {
                first_try = 0;
-                if ( ldap_initialize( &ld, data->url ) != LDAP_SUCCESS ) {
+                if ( ldap_initialize( &ld, data->lm_url ) != LDAP_SUCCESS ) {
                        rc = REWRITE_ERR;
                        goto rc_return;
                }
                goto do_bind;
+
        } else if ( rc != REWRITE_SUCCESS ) {
                rc = REWRITE_ERR;
                goto rc_return;
@@ -289,11 +288,12 @@ do_bind:;
        entry = ldap_first_entry( ld, res );
        assert( entry != NULL );
 
-       if ( data->attrsonly == 1 ) {
+       if ( data->lm_attrsonly == 1 ) {
                /*
                 * dn is newly allocated, so there's no need to strdup it
                 */
                val->bv_val = ldap_get_dn( ld, entry );
+
        } else {
                values = ldap_get_values( ld, entry, lud->lud_attrs[ 0 ] );
                if ( values == NULL || values[ 0 ] == NULL ) {
@@ -317,14 +317,15 @@ do_bind:;
        val->bv_len = strlen( val->bv_val );
 
 rc_return:;
-       if ( data->when == MAP_LDAP_EVERYTIME ) {
+       if ( data->lm_when == MAP_LDAP_EVERYTIME ) {
                if ( ld != NULL ) {
                        ldap_unbind_s( ld );
                }
+
        } else {
-               data->ld = ld;
+               data->lm_ld = ld;
 #ifdef USE_REWRITE_LDAP_PVT_THREADS
-               ldap_pvt_thread_mutex_unlock( &data->mutex );
+               ldap_pvt_thread_mutex_unlock( &data->lm_mutex );
 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
        }
        
@@ -343,9 +344,35 @@ map_ldap_destroy(
        
        data = ( struct ldap_map_data * )(*pmap)->lb_private;
 
+       if ( data->lm_when != MAP_LDAP_EVERYTIME && data->lm_ld != NULL ) {
+               ldap_unbind_s( data->lm_ld );
+               data->lm_ld = NULL;
+       }
+
+       if ( data->lm_lud ) {
+               ldap_free_urldesc( data->lm_lud );
+               data->lm_lud = NULL;
+       }
+
+       if ( data->lm_url ) {
+               free( data->lm_url );
+               data->lm_url = NULL;
+       }
+
+       if ( data->lm_binddn ) {
+               free( data->lm_binddn );
+               data->lm_binddn = NULL;
+       }
+
+       if (data->lm_bindpw ) {
+               memset( data->lm_bindpw, 0, strlen( data->lm_bindpw ) );
+               free( data->lm_bindpw );
+               data->lm_bindpw = NULL;
+       }
+       
        free( data );
-       *pmap = NULL;
+       (*pmap)->lb_private = NULL;
 
        return 0;
 }
-               
+