]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/overlays/rwmdn.c
Sync with HEAD for OL 2.4.5
[openldap] / servers / slapd / overlays / rwmdn.c
index 703a50641f5fe2b94336a42b3ea7a1064a18288f..500993b3fcbfa73599fa098a78d169c823c88579 100644 (file)
@@ -2,7 +2,9 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2003 The OpenLDAP Foundation.
+ * Copyright 1999-2007 The OpenLDAP Foundation.
+ * Portions Copyright 1999-2003 Howard Chu.
+ * Portions Copyright 2000-2003 Pierangelo Masarati.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * <http://www.OpenLDAP.org/license.html>.
  */
 /* ACKNOWLEDGEMENTS:
- * This work was initially developed by the Howard Chu for inclusion
+ * This work was initially developed by Howard Chu for inclusion
  * in OpenLDAP Software and subsequently enhanced by Pierangelo
  * Masarati.
  */
-/* This is an altered version */
 
-/* 
- * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
- * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
- * 
- * Module back-ldap, originally developed by Howard Chu
- *
- * has been modified by Pierangelo Masarati. The original copyright
- * notice has been maintained.
- * 
- * 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:
- * 
- * 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.
- */
 
 #include "portable.h"
 
+#ifdef SLAPD_OVER_RWM
+
 #include <stdio.h>
 
 #include <ac/string.h>
 
 /* FIXME: after rewriting, we should also remap attributes ...  */
 
-#ifdef ENABLE_REWRITE
+/*
+ * massages "in" and normalizes it into "ndn"
+ *
+ * "ndn" may be untouched if no massaging occurred and its value was not null
+ */
+int
+rwm_dn_massage_normalize(
+       dncookie *dc,
+       struct berval *in,
+       struct berval *ndn )
+{
+       int             rc;
+       struct berval   mdn = BER_BVNULL;
+       
+       /* massage and normalize a DN */
+       rc = rwm_dn_massage( dc, in, &mdn );
+       if ( rc != LDAP_SUCCESS ) {
+               return rc;
+       }
+
+       if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( ndn ) ) {
+               return rc;
+       }
+
+       rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
+
+       if ( mdn.bv_val != in->bv_val ) {
+               ch_free( mdn.bv_val );
+       }
+
+       return rc;
+}
+
+/*
+ * massages "in" and prettifies it into "pdn"
+ *
+ * "pdn" may be untouched if no massaging occurred and its value was not null
+ */
+int
+rwm_dn_massage_pretty(
+       dncookie *dc,
+       struct berval *in,
+       struct berval *pdn )
+{
+       int             rc;
+       struct berval   mdn = BER_BVNULL;
+       
+       /* massage and pretty a DN */
+       rc = rwm_dn_massage( dc, in, &mdn );
+       if ( rc != LDAP_SUCCESS ) {
+               return rc;
+       }
+
+       if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
+               return rc;
+       }
+
+       rc = dnPretty( NULL, &mdn, pdn, NULL );
+
+       if ( mdn.bv_val != in->bv_val ) {
+               ch_free( mdn.bv_val );
+       }
+
+       return rc;
+}
+
+/*
+ * massages "in" and prettifies and normalizes it into "pdn" and "ndn"
+ *
+ * "pdn" may be untouched if no massaging occurred and its value was not null;
+ * "ndn" may be untouched if no massaging occurred and its value was not null;
+ * if no massage occurred and "ndn" value was not null, it is filled
+ * with the normaized value of "pdn", much like ndn = dnNormalize( pdn )
+ */
+int
+rwm_dn_massage_pretty_normalize(
+       dncookie *dc,
+       struct berval *in,
+       struct berval *pdn,
+       struct berval *ndn )
+{
+       int             rc;
+       struct berval   mdn = BER_BVNULL;
+       
+       /* massage, pretty and normalize a DN */
+       rc = rwm_dn_massage( dc, in, &mdn );
+       if ( rc != LDAP_SUCCESS ) {
+               return rc;
+       }
+
+       if ( mdn.bv_val == in->bv_val && !BER_BVISNULL( pdn ) ) {
+               if ( BER_BVISNULL( ndn ) ) {
+                       rc = dnNormalize( 0, NULL, NULL, &mdn, ndn, NULL );
+               }
+               return rc;
+       }
+
+       rc = dnPrettyNormal( NULL, &mdn, pdn, ndn, NULL );
+
+       if ( mdn.bv_val != in->bv_val ) {
+               ch_free( mdn.bv_val );
+       }
+
+       return rc;
+}
+
+/*
+ * massages "in" into "dn"
+ * 
+ * "dn" may contain the value of "in" if no massage occurred
+ */
 int
 rwm_dn_massage(
        dncookie *dc,
-       struct berval *dn,
-       struct berval *res
+       struct berval *in,
+       struct berval *dn
 )
 {
-       int rc = 0;
-
-       switch ( rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
-                               (dn->bv_len ? dn->bv_val : ""), 
-                               dc->conn, &res->bv_val ) )
-       {
+       int             rc = 0;
+       struct berval   mdn;
+       static char     *dmy = "";
+
+       assert( dc != NULL );
+       assert( in != NULL );
+       assert( dn != NULL );
+
+       rc = rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
+                       ( in->bv_val ? in->bv_val : dmy ), 
+                       dc->conn, &mdn.bv_val );
+       switch ( rc ) {
        case REWRITE_REGEXEC_OK:
-               if ( res->bv_val != NULL ) {
-                       res->bv_len = strlen( res->bv_val );
+               if ( !BER_BVISNULL( &mdn ) && mdn.bv_val != in->bv_val ) {
+                       mdn.bv_len = strlen( mdn.bv_val );
+                       *dn = mdn;
                } else {
-                       *res = *dn;
+                       *dn = *in;
                }
-#ifdef NEW_LOGGING
-               LDAP_LOG( BACK_LDAP, DETAIL1, 
-                       "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );              
-#else /* !NEW_LOGGING */
-               Debug( LDAP_DEBUG_ARGS,
-                       "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );              
-#endif /* !NEW_LOGGING */
                rc = LDAP_SUCCESS;
+
+               Debug( LDAP_DEBUG_ARGS,
+                       "[rw] %s: \"%s\" -> \"%s\"\n",
+                       dc->ctx, in->bv_val, dn->bv_val );
                break;
                
        case REWRITE_REGEXEC_UNWILLING:
@@ -105,92 +196,16 @@ rwm_dn_massage(
                rc = LDAP_OTHER;
                break;
        }
-       return rc;
-}
-
-#else
-/*
- * rwm_dn_massage
- * 
- * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
- */
-int
-rwm_dn_massage(
-       dncookie *dc,
-       struct berval *odn,
-       struct berval *res
-)
-{
-       int     i, src, dst;
-       struct berval pretty = {0,NULL}, *dn = odn;
-
-       assert( res );
 
-       if ( dn == NULL ) {
-               res->bv_val = NULL;
-               res->bv_len = 0;
-               return 0;
+       if ( mdn.bv_val == dmy ) {
+               BER_BVZERO( &mdn );
        }
-       if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
-               *res = *dn;
-               return 0;
-       }
-
-       if ( dc->tofrom ) {
-               src = 0 + dc->normalized;
-               dst = 2 + dc->normalized;
-       } else {
-               src = 2 + dc->normalized;
-               dst = 0 + dc->normalized;
-               /* DN from remote server may be in arbitrary form.
-                * Pretty it so we can parse reliably.
-                */
-               dnPretty( NULL, dn, &pretty, NULL );
-               if (pretty.bv_val) dn = &pretty;
-       }
-
-       for ( i = 0;
-               dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
-               i += 4 ) {
-               int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
-               int diff = dn->bv_len - aliasLength;
-
-               if ( diff < 0 ) {
-                       /* alias is longer than dn */
-                       continue;
-               } else if ( diff > 0 && ( !DN_SEPARATOR(dn->bv_val[diff-1]))) {
-                       /* boundary is not at a DN separator */
-                       continue;
-                       /* At a DN Separator */
-               }
 
-               if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
-                       res->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
-                       res->bv_val = ch_malloc( res->bv_len + 1 );
-                       strncpy( res->bv_val, dn->bv_val, diff );
-                       strcpy( &res->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
-#ifdef NEW_LOGGING
-                       LDAP_LOG ( BACK_LDAP, ARGS, 
-                               "rwm_dn_massage: converted \"%s\" to \"%s\"\n",
-                               dn->bv_val, res->bv_val, 0 );
-#else
-                       Debug( LDAP_DEBUG_ARGS,
-                               "rwm_dn_massage:"
-                               " converted \"%s\" to \"%s\"\n",
-                               dn->bv_val, res->bv_val, 0 );
-#endif
-                       break;
-               }
-       }
-       if (pretty.bv_val) {
-               ch_free(pretty.bv_val);
-               dn = odn;
-       }
-       /* Nothing matched, just return the original DN */
-       if (res->bv_val == NULL) {
-               *res = *dn;
+       if ( dn->bv_val == dmy ) {
+               BER_BVZERO( dn );
        }
 
-       return 0;
+       return rc;
 }
-#endif /* !ENABLE_REWRITE */
+
+#endif /* SLAPD_OVER_RWM */