Back to index

openldap  2.4.31
params.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 2000-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* ACKNOWLEDGEMENT:
00016  * This work was initially developed by Pierangelo Masarati for
00017  * inclusion in OpenLDAP Software.
00018  */
00019 
00020 #include <portable.h>
00021 
00022 #include "rewrite-int.h"
00023 
00024 /*
00025  * Defines and inits a variable with global scope
00026  */
00027 int
00028 rewrite_param_set(
00029               struct rewrite_info *info,
00030               const char *name,
00031               const char *value
00032 )
00033 {
00034        struct rewrite_var *var;
00035        int rc = REWRITE_SUCCESS;
00036 
00037        assert( info != NULL );
00038        assert( name != NULL );
00039        assert( value != NULL );
00040 
00041 #ifdef USE_REWRITE_LDAP_PVT_THREADS
00042        ldap_pvt_thread_rdwr_wlock( &info->li_params_mutex );
00043 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
00044 
00045        var = rewrite_var_find( info->li_params, name );
00046        if ( var != NULL ) {
00047               assert( var->lv_value.bv_val != NULL );
00048               free( var->lv_value.bv_val );
00049               var->lv_value.bv_val = strdup( value );
00050               var->lv_value.bv_len = strlen( value );
00051 
00052        } else {
00053               var = rewrite_var_insert( &info->li_params, name, value );
00054        }
00055 
00056        if ( var == NULL || var->lv_value.bv_val == NULL ) {
00057               rc = REWRITE_ERR;
00058        }
00059        
00060 #ifdef USE_REWRITE_LDAP_PVT_THREADS
00061        ldap_pvt_thread_rdwr_wunlock( &info->li_params_mutex );
00062 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
00063 
00064        return rc;
00065 }
00066 
00067 /*
00068  * Gets a var with global scope
00069  */
00070 int
00071 rewrite_param_get(
00072               struct rewrite_info *info,
00073               const char *name,
00074               struct berval *value
00075 )
00076 {
00077        struct rewrite_var *var;
00078        int rc = REWRITE_SUCCESS;
00079 
00080        assert( info != NULL );
00081        assert( name != NULL );
00082        assert( value != NULL );
00083 
00084        value->bv_val = NULL;
00085        value->bv_len = 0;
00086        
00087 #ifdef USE_REWRITE_LDAP_PVT_THREADS
00088        ldap_pvt_thread_rdwr_rlock( &info->li_params_mutex );
00089 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
00090        
00091        var = rewrite_var_find( info->li_params, name );
00092        if ( var != NULL ) {
00093               value->bv_val = strdup( var->lv_value.bv_val );
00094               value->bv_len = var->lv_value.bv_len;
00095        }
00096 
00097        if ( var == NULL || value->bv_val == NULL ) {
00098               rc = REWRITE_ERR;
00099        }
00100        
00101 #ifdef USE_REWRITE_LDAP_PVT_THREADS
00102        ldap_pvt_thread_rdwr_runlock( &info->li_params_mutex );
00103 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
00104 
00105        return REWRITE_SUCCESS;
00106 }
00107 
00108 static void
00109 rewrite_param_free(
00110               void *tmp
00111 )
00112 {
00113        struct rewrite_var *var = ( struct rewrite_var * )tmp;
00114        assert( var != NULL );
00115 
00116        assert( var->lv_name != NULL );
00117        assert( var->lv_value.bv_val != NULL );
00118 
00119        free( var->lv_name );
00120        free( var->lv_value.bv_val );
00121        free( var );
00122 }
00123 
00124 /*
00125  * Destroys the parameter tree
00126  */
00127 int
00128 rewrite_param_destroy(
00129               struct rewrite_info *info
00130 )
00131 {
00132        int count;
00133 
00134        assert( info != NULL );
00135        
00136 #ifdef USE_REWRITE_LDAP_PVT_THREADS
00137        ldap_pvt_thread_rdwr_wlock( &info->li_params_mutex );
00138 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
00139        
00140        count = avl_free( info->li_params, rewrite_param_free );
00141        info->li_params = NULL;
00142 
00143 #ifdef USE_REWRITE_LDAP_PVT_THREADS
00144        ldap_pvt_thread_rdwr_wunlock( &info->li_params_mutex );
00145 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
00146 
00147        return REWRITE_SUCCESS;
00148 }
00149