1 /* suffixmassage.c - massages ldap backend dns */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Howard Chu for inclusion
18 * in OpenLDAP Software and subsequently enhanced by Pierangelo
21 /* This is an altered version */
24 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
25 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
27 * Module back-ldap, originally developed by Howard Chu
29 * has been modified by Pierangelo Masarati. The original copyright
30 * notice has been maintained.
32 * Permission is granted to anyone to use this software for any purpose
33 * on any computer system, and to alter it and redistribute it, subject
34 * to the following restrictions:
36 * 1. The author is not responsible for the consequences of use of this
37 * software, no matter how awful, even if they arise from flaws in it.
39 * 2. The origin of this software must not be misrepresented, either by
40 * explicit claim or by omission. Since few users ever read sources,
41 * credits should appear in the documentation.
43 * 3. Altered versions must be plainly marked as such, and must not be
44 * misrepresented as being the original software. Since few users
45 * ever read sources, credits should appear in the documentation.
47 * 4. This notice may not be removed or altered.
54 #include <ac/string.h>
55 #include <ac/socket.h>
58 #include "../back-ldap/back-ldap.h"
59 #include "back-meta.h"
71 switch ( rewrite_session( dc->rwmap->rwm_rw, dc->ctx,
72 ( dn->bv_len ? dn->bv_val : "" ),
73 dc->conn, &res->bv_val ) )
75 case REWRITE_REGEXEC_OK:
76 if ( res->bv_val != NULL ) {
77 res->bv_len = strlen( res->bv_val );
82 LDAP_LOG( BACK_LDAP, DETAIL1,
83 "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );
84 #else /* !NEW_LOGGING */
85 Debug( LDAP_DEBUG_ARGS,
86 "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );
87 #endif /* !NEW_LOGGING */
91 case REWRITE_REGEXEC_UNWILLING:
93 dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
94 dc->rs->sr_text = "Operation not allowed";
96 rc = LDAP_UNWILLING_TO_PERFORM;
99 case REWRITE_REGEXEC_ERR:
101 dc->rs->sr_err = LDAP_OTHER;
102 dc->rs->sr_text = "Rewrite error";
112 * ldap_back_dn_massage
114 * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
117 ldap_back_dn_massage(
124 struct berval pretty = {0,NULL}, *dn = odn;
133 if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
139 src = 0 + dc->normalized;
140 dst = 2 + dc->normalized;
142 src = 2 + dc->normalized;
143 dst = 0 + dc->normalized;
144 /* DN from remote server may be in arbitrary form.
145 * Pretty it so we can parse reliably.
147 dnPretty( NULL, dn, &pretty, NULL );
148 if (pretty.bv_val) dn = &pretty;
152 dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
154 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
155 int diff = dn->bv_len - aliasLength;
158 /* alias is longer than dn */
160 } else if ( diff > 0 && ( !DN_SEPARATOR(dn->bv_val[diff-1]))) {
161 /* boundary is not at a DN separator */
163 /* At a DN Separator */
166 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
167 res->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
168 res->bv_val = ch_malloc( res->bv_len + 1 );
169 strncpy( res->bv_val, dn->bv_val, diff );
170 strcpy( &res->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
172 LDAP_LOG ( BACK_LDAP, ARGS,
173 "ldap_back_dn_massage: converted \"%s\" to \"%s\"\n",
174 dn->bv_val, res->bv_val, 0 );
176 Debug( LDAP_DEBUG_ARGS,
177 "ldap_back_dn_massage:"
178 " converted \"%s\" to \"%s\"\n",
179 dn->bv_val, res->bv_val, 0 );
185 ch_free(pretty.bv_val);
188 /* Nothing matched, just return the original DN */
189 if (res->bv_val == NULL) {
195 #endif /* !ENABLE_REWRITE */