]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/suffixmassage.c
Berkeley DB 4.2 support (DB 4.2 required by default)
[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/string.h>
36 #include <ac/socket.h>
37
38 #include "slap.h"
39 #include "back-ldap.h"
40
41 #ifdef ENABLE_REWRITE
42 int
43 ldap_back_dn_massage(
44         dncookie *dc,
45         struct berval *dn,
46         struct berval *res
47 )
48 {
49         int rc = 0;
50
51         switch (rewrite_session( dc->rwmap->rwm_rw, dc->ctx, (dn->bv_len ? dn->bv_val : ""), dc->conn, 
52                                 &res->bv_val )) {
53         case REWRITE_REGEXEC_OK:
54                 if ( res->bv_val != NULL ) {
55                         res->bv_len = strlen( res->bv_val );
56                 } else {
57                         *res = *dn;
58                 }
59 #ifdef NEW_LOGGING
60                 LDAP_LOG( BACK_LDAP, DETAIL1, 
61                         "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );              
62 #else /* !NEW_LOGGING */
63                 Debug( LDAP_DEBUG_ARGS,
64                         "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );              
65 #endif /* !NEW_LOGGING */
66                 rc = LDAP_SUCCESS;
67                 break;
68                 
69         case REWRITE_REGEXEC_UNWILLING:
70                 if ( dc->rs ) {
71                         dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
72                         dc->rs->sr_text = "Operation not allowed";
73                 }
74                 rc = LDAP_UNWILLING_TO_PERFORM;
75                 break;
76                 
77         case REWRITE_REGEXEC_ERR:
78                 if ( dc->rs ) {
79                         dc->rs->sr_err = LDAP_OTHER;
80                         dc->rs->sr_text = "Rewrite error";
81                 }
82                 rc = LDAP_OTHER;
83                 break;
84         }
85         return rc;
86 }
87
88 #else
89 /*
90  * ldap_back_dn_massage
91  * 
92  * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
93  */
94 int
95 ldap_back_dn_massage(
96         dncookie *dc,
97         struct berval *odn,
98         struct berval *res
99 )
100 {
101         int     i, src, dst;
102         struct berval pretty = {0,NULL}, *dn = odn;
103
104         assert( res );
105
106         if ( dn == NULL ) {
107                 res->bv_val = NULL;
108                 res->bv_len = 0;
109                 return 0;
110         }
111         if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
112                 *res = *dn;
113                 return 0;
114         }
115
116         if ( dc->tofrom ) {
117                 src = 0 + dc->normalized;
118                 dst = 2 + dc->normalized;
119         } else {
120                 src = 2 + dc->normalized;
121                 dst = 0 + dc->normalized;
122                 /* DN from remote server may be in arbitrary form.
123                  * Pretty it so we can parse reliably.
124                  */
125                 dnPretty( NULL, dn, &pretty, NULL );
126                 if (pretty.bv_val) dn = &pretty;
127         }
128
129         for ( i = 0;
130                 dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
131                 i += 4 ) {
132                 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
133                 int diff = dn->bv_len - aliasLength;
134
135                 if ( diff < 0 ) {
136                         /* alias is longer than dn */
137                         continue;
138                 } else if ( diff > 0 && ( !DN_SEPARATOR(dn->bv_val[diff-1]))) {
139                         /* boundary is not at a DN separator */
140                         continue;
141                         /* At a DN Separator */
142                 }
143
144                 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
145                         res->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
146                         res->bv_val = ch_malloc( res->bv_len + 1 );
147                         strncpy( res->bv_val, dn->bv_val, diff );
148                         strcpy( &res->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
149 #ifdef NEW_LOGGING
150                         LDAP_LOG ( BACK_LDAP, ARGS, 
151                                 "ldap_back_dn_massage: converted \"%s\" to \"%s\"\n",
152                                 dn->bv_val, res->bv_val, 0 );
153 #else
154                         Debug( LDAP_DEBUG_ARGS,
155                                 "ldap_back_dn_massage:"
156                                 " converted \"%s\" to \"%s\"\n",
157                                 dn->bv_val, res->bv_val, 0 );
158 #endif
159                         break;
160                 }
161         }
162         if (pretty.bv_val) {
163                 ch_free(pretty.bv_val);
164                 dn = odn;
165         }
166         /* Nothing matched, just return the original DN */
167         if (res->bv_val == NULL) {
168                 *res = *dn;
169         }
170
171         return 0;
172 }
173 #endif /* !ENABLE_REWRITE */