]> git.sur5r.net Git - openldap/blobdiff - libraries/librewrite/map.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / librewrite / map.c
index d17d8146299bf7e74b1d34454151b3596a164848..c467e471f6ad10ad2a8cb46ea2a6dd17bf795d00 100644 (file)
@@ -1,26 +1,21 @@
-/******************************************************************************
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
+ * Copyright 2000-2013 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>
 
 #include "rewrite-int.h"
 #include "rewrite-map.h"
 
+static int num_mappers;
+static const rewrite_mapper **mappers;
+#define        MAPPER_ALLOC    8
+
 struct rewrite_map *
 rewrite_map_parse(
                struct rewrite_info *info,
@@ -56,7 +55,7 @@ rewrite_map_parse(
         * Go to the end of the map invocation (the right closing brace)
         */
        for ( p = string, cnt = 1; p[ 0 ] != '\0' && cnt > 0; p++ ) {
-               if ( p[ 0 ] == REWRITE_SUBMATCH_ESCAPE ) {
+               if ( IS_REWRITE_SUBMATCH_ESCAPE( p[ 0 ] ) ) {
                        /*
                         * '%' marks the beginning of a new map
                         */
@@ -89,6 +88,9 @@ rewrite_map_parse(
         */
        l = p - string - 1;
        s = calloc( sizeof( char ), l + 1 );
+       if ( s == NULL ) {
+               return NULL;
+       }
        AC_MEMCPY( s, string, l );
        s[ l ] = 0;
 
@@ -232,6 +234,10 @@ rewrite_map_parse(
                 */
                map->lm_type = REWRITE_MAP_SUBCONTEXT;
                map->lm_name = strdup( s + 1 );
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                map->lm_data = rewrite_context_find( info, s + 1 );
                if ( map->lm_data == NULL ) {
                        rc = -1;
@@ -267,6 +273,10 @@ rewrite_map_parse(
                                map->lm_name = strdup( s + 1 );
                        }
                }
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                break;
        
        /*
@@ -280,6 +290,10 @@ rewrite_map_parse(
                        map->lm_type = REWRITE_MAP_GET_OP_VAR;
                        map->lm_name = strdup( s + 1 );
                }
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                break;
        
        /*
@@ -288,6 +302,10 @@ rewrite_map_parse(
        case REWRITE_OPERATOR_PARAM_GET:                /* '$' */
                map->lm_type = REWRITE_MAP_GET_PARAM;
                map->lm_name = strdup( s + 1 );
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                break;
        
        /*
@@ -296,6 +314,10 @@ rewrite_map_parse(
        default:
                map->lm_type = REWRITE_MAP_BUILTIN;
                map->lm_name = strdup( s );
+               if ( map->lm_name == NULL ) {
+                       rc = -1;
+                       goto cleanup;
+               }
                map->lm_data = rewrite_builtin_map_find( info, s );
                if ( map->lm_data == NULL ) {
                        rc = -1;
@@ -359,7 +381,12 @@ rewrite_map_apply(
                                ( struct rewrite_context * )map->lm_data,
                                key->bv_val, &val->bv_val );
                if ( val->bv_val != NULL ) {
-                       val->bv_len = strlen( val->bv_val );
+                       if ( val->bv_val == key->bv_val ) {
+                               val->bv_len = key->bv_len;
+                               key->bv_val = NULL;
+                       } else {
+                               val->bv_len = strlen( val->bv_val );
+                       }
                }
                break;
 
@@ -368,11 +395,16 @@ rewrite_map_apply(
                rc = rewrite_var_set( &op->lo_vars, map->lm_name,
                                key->bv_val, 1 )
                        ? REWRITE_SUCCESS : REWRITE_ERR;
-               if ( map->lm_type == REWRITE_MAP_SET_OP_VAR ) {
-                       val->bv_val = strdup( "" );
-               } else {
-                       val->bv_val = strdup( key->bv_val );
-                       val->bv_len = key->bv_len;
+               if ( rc == REWRITE_SUCCESS ) {
+                       if ( map->lm_type == REWRITE_MAP_SET_OP_VAR ) {
+                               val->bv_val = strdup( "" );
+                       } else {
+                               val->bv_val = strdup( key->bv_val );
+                               val->bv_len = key->bv_len;
+                       }
+                       if ( val->bv_val == NULL ) {
+                               rc = REWRITE_ERR;
+                       }
                }
                break;
        
@@ -385,6 +417,9 @@ rewrite_map_apply(
                } else {
                        val->bv_val = strdup( var->lv_value.bv_val );
                        val->bv_len = var->lv_value.bv_len;
+                       if ( val->bv_val == NULL ) {
+                               rc = REWRITE_ERR;
+                       }
                }
                break;  
        }
@@ -397,11 +432,16 @@ rewrite_map_apply(
                }
                rc = rewrite_session_var_set( info, op->lo_cookie, 
                                map->lm_name, key->bv_val );
-               if ( map->lm_type == REWRITE_MAP_SET_SESN_VAR ) {
-                       val->bv_val = strdup( "" );
-               } else {
-                       val->bv_val = strdup( key->bv_val );
-                       val->bv_len = key->bv_len;
+               if ( rc == REWRITE_SUCCESS ) {
+                       if ( map->lm_type == REWRITE_MAP_SET_SESN_VAR ) {
+                               val->bv_val = strdup( "" );
+                       } else {
+                               val->bv_val = strdup( key->bv_val );
+                               val->bv_len = key->bv_len;
+                       }
+                       if ( val->bv_val == NULL ) {
+                               rc = REWRITE_ERR;
+                       }
                }
                break;
 
@@ -417,14 +457,12 @@ rewrite_map_apply(
        case REWRITE_MAP_BUILTIN: {
                struct rewrite_builtin_map *bmap = map->lm_data;
 
-               switch ( bmap->lb_type ) {
-               case REWRITE_BUILTIN_MAP_LDAP:
-                       rc = map_ldap_apply( bmap, key->bv_val, val );
-                       break;
-               default:
+               if ( bmap->lb_mapper && bmap->lb_mapper->rm_apply )
+                       rc = bmap->lb_mapper->rm_apply( bmap->lb_private, key->bv_val,
+                               val );
+               else
                        rc = REWRITE_ERR;
                        break;
-               }
                break;
        }
 
@@ -443,17 +481,10 @@ rewrite_builtin_map_free(
 {
        struct rewrite_builtin_map *map = ( struct rewrite_builtin_map * )tmp;
 
-       assert( map );
-
-       switch ( map->lb_type ) {
-       case REWRITE_BUILTIN_MAP_LDAP:
-               map_ldap_destroy( &map );
-               break;
+       assert( map != NULL );
 
-       default:
-               assert(0);
-               break;
-       }
+       if ( map->lb_mapper && map->lb_mapper->rm_destroy )
+               map->lb_mapper->rm_destroy( map->lb_private );
 
        free( map->lb_name );
        free( map );
@@ -466,8 +497,8 @@ rewrite_map_destroy(
 {
        struct rewrite_map *map;
        
-       assert( pmap );
-       assert( *pmap );
+       assert( pmap != NULL );
+       assert( *pmap != NULL );
 
        map = *pmap;
 
@@ -495,3 +526,58 @@ rewrite_map_destroy(
        return 0;
 }
 
+/* ldapmap.c */
+extern const rewrite_mapper rewrite_ldap_mapper;
+
+const rewrite_mapper *
+rewrite_mapper_find(
+       const char *name
+)
+{
+       int i;
+
+       if ( !strcasecmp( name, "ldap" ))
+               return &rewrite_ldap_mapper;
+
+       for (i=0; i<num_mappers; i++)
+               if ( !strcasecmp( name, mappers[i]->rm_name ))
+                       return mappers[i];
+       return NULL;
+}
+
+int
+rewrite_mapper_register(
+       const rewrite_mapper *map
+)
+{
+       if ( num_mappers % MAPPER_ALLOC == 0 ) {
+               const rewrite_mapper **mnew;
+               mnew = realloc( mappers, (num_mappers + MAPPER_ALLOC) *
+                       sizeof( rewrite_mapper * ));
+               if ( mnew )
+                       mappers = mnew;
+               else
+                       return -1;
+       }
+       mappers[num_mappers++] = map;
+       return 0;
+}
+
+int
+rewrite_mapper_unregister(
+       const rewrite_mapper *map
+)
+{
+       int i;
+
+       for (i = 0; i<num_mappers; i++) {
+               if ( mappers[i] == map ) {
+                       num_mappers--;
+                       mappers[i] = mappers[num_mappers];
+                       mappers[num_mappers] = NULL;
+                       return 0;
+               }
+       }
+       /* not found */
+       return -1;
+}