Back to index

openldap  2.4.31
suffixmassage.c
Go to the documentation of this file.
00001 /* suffixmassage.c - massages ldap backend dns */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2003-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by the Howard Chu for inclusion
00018  * in OpenLDAP Software and subsequently enhanced by Pierangelo
00019  * Masarati.
00020  */
00021 /* This is an altered version */
00022 
00023 /* 
00024  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
00025  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
00026  * 
00027  * Module back-ldap, originally developed by Howard Chu
00028  *
00029  * has been modified by Pierangelo Masarati. The original copyright
00030  * notice has been maintained.
00031  * 
00032  * Permission is granted to anyone to use this software for any purpose
00033  * on any computer system, and to alter it and redistribute it, subject
00034  * to the following restrictions:
00035  * 
00036  * 1. The author is not responsible for the consequences of use of this
00037  *    software, no matter how awful, even if they arise from flaws in it.
00038  * 
00039  * 2. The origin of this software must not be misrepresented, either by
00040  *    explicit claim or by omission.  Since few users ever read sources,
00041  *    credits should appear in the documentation.
00042  * 
00043  * 3. Altered versions must be plainly marked as such, and must not be
00044  *    misrepresented as being the original software.  Since few users
00045  *    ever read sources, credits should appear in the documentation.
00046  * 
00047  * 4. This notice may not be removed or altered.
00048  */
00049 
00050 #include "portable.h"
00051 
00052 #include <stdio.h>
00053 
00054 #include <ac/string.h>
00055 #include <ac/socket.h>
00056 
00057 #include "slap.h"
00058 #include "../back-ldap/back-ldap.h"
00059 #include "back-meta.h"
00060 
00061 #ifdef ENABLE_REWRITE
00062 int
00063 ldap_back_dn_massage(
00064        dncookie      *dc,
00065        struct berval *dn,
00066        struct berval *res )
00067 {
00068        int           rc = 0;
00069        static char   *dmy = "";
00070 
00071        switch ( rewrite_session( dc->target->mt_rwmap.rwm_rw, dc->ctx,
00072                             ( dn->bv_val ? dn->bv_val : dmy ),
00073                             dc->conn, &res->bv_val ) )
00074        {
00075        case REWRITE_REGEXEC_OK:
00076               if ( res->bv_val != NULL ) {
00077                      res->bv_len = strlen( res->bv_val );
00078               } else {
00079                      *res = *dn;
00080               }
00081               Debug( LDAP_DEBUG_ARGS,
00082                      "[rw] %s: \"%s\" -> \"%s\"\n",
00083                      dc->ctx,
00084                      BER_BVISNULL( dn ) ? "" : dn->bv_val,
00085                      BER_BVISNULL( res ) ? "" : res->bv_val );
00086               rc = LDAP_SUCCESS;
00087               break;
00088               
00089        case REWRITE_REGEXEC_UNWILLING:
00090               if ( dc->rs ) {
00091                      dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
00092                      dc->rs->sr_text = "Operation not allowed";
00093               }
00094               rc = LDAP_UNWILLING_TO_PERFORM;
00095               break;
00096                      
00097        case REWRITE_REGEXEC_ERR:
00098               if ( dc->rs ) {
00099                      dc->rs->sr_err = LDAP_OTHER;
00100                      dc->rs->sr_text = "Rewrite error";
00101               }
00102               rc = LDAP_OTHER;
00103               break;
00104        }
00105 
00106        if ( res->bv_val == dmy ) { 
00107               BER_BVZERO( res );
00108        }
00109 
00110        return rc;
00111 }
00112 
00113 #else
00114 /*
00115  * ldap_back_dn_massage
00116  * 
00117  * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
00118  */
00119 int
00120 ldap_back_dn_massage(
00121        dncookie *dc,
00122        struct berval *odn,
00123        struct berval *res
00124 )
00125 {
00126        int     i, src, dst;
00127        struct berval pretty = {0,NULL}, *dn = odn;
00128 
00129        assert( res != NULL );
00130 
00131        if ( dn == NULL ) {
00132               res->bv_val = NULL;
00133               res->bv_len = 0;
00134               return 0;
00135        }
00136        if ( dc->target->mt_rwmap.rwm_suffix_massage == NULL ) {
00137               *res = *dn;
00138               return 0;
00139        }
00140 
00141        if ( dc->tofrom ) {
00142               src = 0 + dc->normalized;
00143               dst = 2 + dc->normalized;
00144        } else {
00145               src = 2 + dc->normalized;
00146               dst = 0 + dc->normalized;
00147               /* DN from remote server may be in arbitrary form.
00148                * Pretty it so we can parse reliably.
00149                */
00150               dnPretty( NULL, dn, &pretty, NULL );
00151               if (pretty.bv_val) dn = &pretty;
00152        }
00153 
00154        for ( i = 0;
00155               dc->target->mt_rwmap.rwm_suffix_massage[i].bv_val != NULL;
00156               i += 4 ) {
00157               int aliasLength = dc->target->mt_rwmap.rwm_suffix_massage[i+src].bv_len;
00158               int diff = dn->bv_len - aliasLength;
00159 
00160               if ( diff < 0 ) {
00161                      /* alias is longer than dn */
00162                      continue;
00163               } else if ( diff > 0 && ( !DN_SEPARATOR(dn->bv_val[diff-1]))) {
00164                      /* boundary is not at a DN separator */
00165                      continue;
00166                      /* At a DN Separator */
00167               }
00168 
00169               if ( !strcmp( dc->target->mt_rwmap.rwm_suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
00170                      res->bv_len = diff + dc->target->mt_rwmap.rwm_suffix_massage[i+dst].bv_len;
00171                      res->bv_val = ch_malloc( res->bv_len + 1 );
00172                      strncpy( res->bv_val, dn->bv_val, diff );
00173                      strcpy( &res->bv_val[diff], dc->target->mt_rwmap.rwm_suffix_massage[i+dst].bv_val );
00174                      Debug( LDAP_DEBUG_ARGS,
00175                             "ldap_back_dn_massage:"
00176                             " converted \"%s\" to \"%s\"\n",
00177                             BER_BVISNULL( dn ) ? "" : dn->bv_val,
00178                             BER_BVISNULL( res ) ? "" : res->bv_val, 0 );
00179                      break;
00180               }
00181        }
00182        if (pretty.bv_val) {
00183               ch_free(pretty.bv_val);
00184               dn = odn;
00185        }
00186        /* Nothing matched, just return the original DN */
00187        if (res->bv_val == NULL) {
00188               *res = *dn;
00189        }
00190 
00191        return 0;
00192 }
00193 #endif /* !ENABLE_REWRITE */