Back to index

openldap  2.4.31
rwmdn.c
Go to the documentation of this file.
00001 /* rwmdn.c - massages dns */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1999-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 1999-2003 Howard Chu.
00007  * Portions Copyright 2000-2003 Pierangelo Masarati.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted only as authorized by the OpenLDAP
00012  * Public License.
00013  *
00014  * A copy of this license is available in the file LICENSE in the
00015  * top-level directory of the distribution or, alternatively, at
00016  * <http://www.OpenLDAP.org/license.html>.
00017  */
00018 /* ACKNOWLEDGEMENTS:
00019  * This work was initially developed by Howard Chu for inclusion
00020  * in OpenLDAP Software and subsequently enhanced by Pierangelo
00021  * Masarati.
00022  */
00023 
00024 
00025 #include "portable.h"
00026 
00027 #ifdef SLAPD_OVER_RWM
00028 
00029 #include <stdio.h>
00030 
00031 #include <ac/string.h>
00032 #include <ac/socket.h>
00033 
00034 #include "slap.h"
00035 #include "rwm.h"
00036 
00037 /* FIXME: after rewriting, we should also remap attributes ...  */
00038 
00039 /*
00040  * massages "in" and normalizes it into "ndn"
00041  *
00042  * "ndn" may be untouched if no massaging occurred and its value was not null
00043  */
00044 int
00045 rwm_dn_massage_normalize(
00046        dncookie *dc,
00047        struct berval *in,
00048        struct berval *ndn )
00049 {
00050        int           rc;
00051        struct berval mdn = BER_BVNULL;
00052        
00053        /* massage and normalize a DN */
00054        rc = rwm_dn_massage( dc, in, &mdn );
00055        if ( rc != LDAP_SUCCESS ) {
00056               return rc;
00057        }
00058 
00059        if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( ndn ) ) {
00060               return rc;
00061        }
00062 
00063        rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
00064 
00065        if ( mdn.bv_val != in->bv_val ) {
00066               ch_free( mdn.bv_val );
00067        }
00068 
00069        return rc;
00070 }
00071 
00072 /*
00073  * massages "in" and prettifies it into "pdn"
00074  *
00075  * "pdn" may be untouched if no massaging occurred and its value was not null
00076  */
00077 int
00078 rwm_dn_massage_pretty(
00079        dncookie *dc,
00080        struct berval *in,
00081        struct berval *pdn )
00082 {
00083        int           rc;
00084        struct berval mdn = BER_BVNULL;
00085        
00086        /* massage and pretty a DN */
00087        rc = rwm_dn_massage( dc, in, &mdn );
00088        if ( rc != LDAP_SUCCESS ) {
00089               return rc;
00090        }
00091 
00092        if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
00093               return rc;
00094        }
00095 
00096        rc = dnPretty( NULL, &mdn, pdn, NULL );
00097 
00098        if ( mdn.bv_val != in->bv_val ) {
00099               ch_free( mdn.bv_val );
00100        }
00101 
00102        return rc;
00103 }
00104 
00105 /*
00106  * massages "in" and prettifies and normalizes it into "pdn" and "ndn"
00107  *
00108  * "pdn" may be untouched if no massaging occurred and its value was not null;
00109  * "ndn" may be untouched if no massaging occurred and its value was not null;
00110  * if no massage occurred and "ndn" value was not null, it is filled
00111  * with the normaized value of "pdn", much like ndn = dnNormalize( pdn )
00112  */
00113 int
00114 rwm_dn_massage_pretty_normalize(
00115        dncookie *dc,
00116        struct berval *in,
00117        struct berval *pdn,
00118        struct berval *ndn )
00119 {
00120        int           rc;
00121        struct berval mdn = BER_BVNULL;
00122        
00123        /* massage, pretty and normalize a DN */
00124        rc = rwm_dn_massage( dc, in, &mdn );
00125        if ( rc != LDAP_SUCCESS ) {
00126               return rc;
00127        }
00128 
00129        if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
00130               if ( BER_BVISNULL( ndn ) ) {
00131                      rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
00132               }
00133               return rc;
00134        }
00135 
00136        rc = dnPrettyNormal( NULL, &mdn, pdn, ndn, NULL );
00137 
00138        if ( mdn.bv_val != in->bv_val ) {
00139               ch_free( mdn.bv_val );
00140        }
00141 
00142        return rc;
00143 }
00144 
00145 /*
00146  * massages "in" into "dn"
00147  * 
00148  * "dn" may contain the value of "in" if no massage occurred
00149  */
00150 int
00151 rwm_dn_massage(
00152        dncookie *dc,
00153        struct berval *in,
00154        struct berval *dn
00155 )
00156 {
00157        int           rc = 0;
00158        struct berval mdn;
00159        static char   *dmy = "";
00160        char *in_val;
00161 
00162        assert( dc != NULL );
00163        assert( in != NULL );
00164        assert( dn != NULL );
00165 
00166        /* protect from NULL berval */
00167        in_val = in->bv_val ? in->bv_val : dmy;
00168 
00169        rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
00170                      in_val, dc->conn, &mdn.bv_val );
00171        switch ( rc ) {
00172        case REWRITE_REGEXEC_OK:
00173               if ( !BER_BVISNULL( &mdn ) && mdn.bv_val != in_val ) {
00174                      mdn.bv_len = strlen( mdn.bv_val );
00175                      *dn = mdn;
00176               } else {
00177                      dn->bv_len = in->bv_len;
00178                      dn->bv_val = in_val;
00179               }
00180               rc = LDAP_SUCCESS;
00181 
00182               Debug( LDAP_DEBUG_ARGS,
00183                      "[rw] %s: \"%s\" -> \"%s\"\n",
00184                      dc->ctx, in_val, dn->bv_val );
00185               break;
00186               
00187        case REWRITE_REGEXEC_UNWILLING:
00188               if ( dc->rs ) {
00189                      dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
00190                      dc->rs->sr_text = "Operation not allowed";
00191               }
00192               rc = LDAP_UNWILLING_TO_PERFORM;
00193               break;
00194                      
00195        case REWRITE_REGEXEC_ERR:
00196               if ( dc->rs ) {
00197                      dc->rs->sr_err = LDAP_OTHER;
00198                      dc->rs->sr_text = "Rewrite error";
00199               }
00200               rc = LDAP_OTHER;
00201               break;
00202        }
00203 
00204        if ( mdn.bv_val == dmy ) {
00205               BER_BVZERO( &mdn );
00206        }
00207 
00208        if ( dn->bv_val == dmy ) {
00209               BER_BVZERO( dn );
00210        }
00211 
00212        return rc;
00213 }
00214 
00215 #endif /* SLAPD_OVER_RWM */