]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/suffixmassage.c
Free IDL_CACHE locks
[openldap] / servers / slapd / back-ldap / suffixmassage.c
1 /* suffixmassage.c - massages ldap backend dns */
2 /* $OpenLDAP$ */
3
4 /* 
5  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
6  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
7  * 
8  * Module back-ldap, originally developed by Howard Chu
9  *
10  * has been modified by Pierangelo Masarati. The original copyright
11  * notice has been maintained.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author is not responsible for the consequences of use of this
18  *    software, no matter how awful, even if they arise from flaws in it.
19  * 
20  * 2. The origin of this software must not be misrepresented, either by
21  *    explicit claim or by omission.  Since few users ever read sources,
22  *    credits should appear in the documentation.
23  * 
24  * 3. Altered versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.  Since few users
26  *    ever read sources, credits should appear in the documentation.
27  * 
28  * 4. This notice may not be removed or altered.
29  */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34
35 #include <ac/socket.h>
36
37 #include "slap.h"
38 #include "back-ldap.h"
39
40 #ifdef ENABLE_REWRITE
41 int
42 ldap_back_dn_massage(
43         dncookie *dc,
44         struct berval *dn,
45         struct berval *res
46 )
47 {
48         int rc = 0;
49
50         switch (rewrite_session( dc->rwmap->rwm_rw, dc->ctx, (dn->bv_len ? dn->bv_val : ""), dc->conn, 
51                                 &res->bv_val )) {
52         case REWRITE_REGEXEC_OK:
53                 if ( res->bv_val != NULL ) {
54                         res->bv_len = strlen( res->bv_val );
55                 } else {
56                         *res = *dn;
57                 }
58 #ifdef NEW_LOGGING
59                 LDAP_LOG( BACK_LDAP, DETAIL1, 
60                         "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );              
61 #else /* !NEW_LOGGING */
62                 Debug( LDAP_DEBUG_ARGS,
63                         "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );              
64 #endif /* !NEW_LOGGING */
65                 rc = LDAP_SUCCESS;
66                 break;
67                 
68         case REWRITE_REGEXEC_UNWILLING:
69                 if ( dc->rs ) {
70                         dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
71                         dc->rs->sr_text = "Operation not allowed";
72                 }
73                 rc = LDAP_UNWILLING_TO_PERFORM;
74                 break;
75                 
76         case REWRITE_REGEXEC_ERR:
77                 if ( dc->rs ) {
78                         dc->rs->sr_err = LDAP_OTHER;
79                         dc->rs->sr_text = "Rewrite error";
80                 }
81                 rc = LDAP_OTHER;
82                 break;
83         }
84         return rc;
85 }
86
87 #else
88 /*
89  * ldap_back_dn_massage
90  * 
91  * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
92  */
93 int
94 ldap_back_dn_massage(
95         dncookie *dc,
96         struct berval *odn,
97         struct berval *res
98 )
99 {
100         int     i, src, dst;
101         struct berval pretty = {0,NULL}, *dn = odn;
102
103         assert( res );
104
105         if ( dn == NULL ) {
106                 res->bv_val = NULL;
107                 res->bv_len = 0;
108                 return 0;
109         }
110         if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
111                 *res = *dn;
112                 return 0;
113         }
114
115         if ( dc->tofrom ) {
116                 src = 0 + dc->normalized;
117                 dst = 2 + dc->normalized;
118         } else {
119                 src = 2 + dc->normalized;
120                 dst = 0 + dc->normalized;
121                 /* DN from remote server may be in arbitrary form.
122                  * Pretty it so we can parse reliably.
123                  */
124                 dnPretty2( NULL, dn, &pretty, NULL );
125                 if (pretty.bv_val) dn = &pretty;
126         }
127
128         for ( i = 0;
129                 dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
130                 i += 4 ) {
131                 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
132                 int diff = dn->bv_len - aliasLength;
133
134                 if ( diff < 0 ) {
135                         /* alias is longer than dn */
136                         continue;
137                 } else if ( diff > 0 && ( !DN_SEPARATOR(dn->bv_val[diff-1]))) {
138                         /* boundary is not at a DN separator */
139                         continue;
140                         /* At a DN Separator */
141                 }
142
143                 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
144                         res->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
145                         res->bv_val = ch_malloc( res->bv_len + 1 );
146                         strncpy( res->bv_val, dn->bv_val, diff );
147                         strcpy( &res->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
148 #ifdef NEW_LOGGING
149                         LDAP_LOG ( BACK_LDAP, ARGS, 
150                                 "ldap_back_dn_massage: converted \"%s\" to \"%s\"\n",
151                                 dn->bv_val, res->bv_val, 0 );
152 #else
153                         Debug( LDAP_DEBUG_ARGS,
154                                 "ldap_back_dn_massage:"
155                                 " converted \"%s\" to \"%s\"\n",
156                                 dn->bv_val, res->bv_val, 0 );
157 #endif
158                         break;
159                 }
160         }
161         if (pretty.bv_val) {
162                 ch_free(pretty.bv_val);
163                 dn = odn;
164         }
165         /* Nothing matched, just return the original DN */
166         if (res->bv_val == NULL) {
167                 *res = *dn;
168         }
169
170         return 0;
171 }
172 #endif /* !ENABLE_REWRITE */