]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/suffixmassage.c
05e4ca21834fcb76777008b4e0090e82a2b89f04
[openldap] / servers / slapd / back-meta / suffixmassage.c
1 /* suffixmassage.c - massages ldap backend dns */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by the Howard Chu for inclusion
18  * in OpenLDAP Software and subsequently enhanced by Pierangelo
19  * Masarati.
20  */
21 /* This is an altered version */
22
23 /* 
24  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
25  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
26  * 
27  * Module back-ldap, originally developed by Howard Chu
28  *
29  * has been modified by Pierangelo Masarati. The original copyright
30  * notice has been maintained.
31  * 
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:
35  * 
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.
38  * 
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.
42  * 
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.
46  * 
47  * 4. This notice may not be removed or altered.
48  */
49
50 #include "portable.h"
51
52 #include <stdio.h>
53
54 #include <ac/string.h>
55 #include <ac/socket.h>
56
57 #include "slap.h"
58 #include "../back-ldap/back-ldap.h"
59 #include "back-meta.h"
60
61 #ifdef ENABLE_REWRITE
62 int
63 ldap_back_dn_massage(
64         dncookie *dc,
65         struct berval *dn,
66         struct berval *res
67 )
68 {
69         int rc = 0;
70
71         switch (rewrite_session( dc->rwmap->rwm_rw, dc->ctx, (dn->bv_len ? dn->bv_val : ""), dc->conn, 
72                                 &res->bv_val )) {
73         case REWRITE_REGEXEC_OK:
74                 if ( res->bv_val != NULL ) {
75                         res->bv_len = strlen( res->bv_val );
76                 } else {
77                         *res = *dn;
78                 }
79 #ifdef NEW_LOGGING
80                 LDAP_LOG( BACK_LDAP, DETAIL1, 
81                         "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );              
82 #else /* !NEW_LOGGING */
83                 Debug( LDAP_DEBUG_ARGS,
84                         "[rw] %s: \"%s\" -> \"%s\"\n", dc->ctx, dn->bv_val, res->bv_val );              
85 #endif /* !NEW_LOGGING */
86                 rc = LDAP_SUCCESS;
87                 break;
88                 
89         case REWRITE_REGEXEC_UNWILLING:
90                 if ( dc->rs ) {
91                         dc->rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
92                         dc->rs->sr_text = "Operation not allowed";
93                 }
94                 rc = LDAP_UNWILLING_TO_PERFORM;
95                 break;
96                 
97         case REWRITE_REGEXEC_ERR:
98                 if ( dc->rs ) {
99                         dc->rs->sr_err = LDAP_OTHER;
100                         dc->rs->sr_text = "Rewrite error";
101                 }
102                 rc = LDAP_OTHER;
103                 break;
104         }
105         return rc;
106 }
107
108 #else
109 /*
110  * ldap_back_dn_massage
111  * 
112  * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
113  */
114 int
115 ldap_back_dn_massage(
116         dncookie *dc,
117         struct berval *odn,
118         struct berval *res
119 )
120 {
121         int     i, src, dst;
122         struct berval pretty = {0,NULL}, *dn = odn;
123
124         assert( res );
125
126         if ( dn == NULL ) {
127                 res->bv_val = NULL;
128                 res->bv_len = 0;
129                 return 0;
130         }
131         if ( dc->rwmap == NULL || dc->rwmap->rwm_suffix_massage == NULL ) {
132                 *res = *dn;
133                 return 0;
134         }
135
136         if ( dc->tofrom ) {
137                 src = 0 + dc->normalized;
138                 dst = 2 + dc->normalized;
139         } else {
140                 src = 2 + dc->normalized;
141                 dst = 0 + dc->normalized;
142                 /* DN from remote server may be in arbitrary form.
143                  * Pretty it so we can parse reliably.
144                  */
145                 dnPretty( NULL, dn, &pretty, NULL );
146                 if (pretty.bv_val) dn = &pretty;
147         }
148
149         for ( i = 0;
150                 dc->rwmap->rwm_suffix_massage[i].bv_val != NULL;
151                 i += 4 ) {
152                 int aliasLength = dc->rwmap->rwm_suffix_massage[i+src].bv_len;
153                 int diff = dn->bv_len - aliasLength;
154
155                 if ( diff < 0 ) {
156                         /* alias is longer than dn */
157                         continue;
158                 } else if ( diff > 0 && ( !DN_SEPARATOR(dn->bv_val[diff-1]))) {
159                         /* boundary is not at a DN separator */
160                         continue;
161                         /* At a DN Separator */
162                 }
163
164                 if ( !strcmp( dc->rwmap->rwm_suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
165                         res->bv_len = diff + dc->rwmap->rwm_suffix_massage[i+dst].bv_len;
166                         res->bv_val = ch_malloc( res->bv_len + 1 );
167                         strncpy( res->bv_val, dn->bv_val, diff );
168                         strcpy( &res->bv_val[diff], dc->rwmap->rwm_suffix_massage[i+dst].bv_val );
169 #ifdef NEW_LOGGING
170                         LDAP_LOG ( BACK_LDAP, ARGS, 
171                                 "ldap_back_dn_massage: converted \"%s\" to \"%s\"\n",
172                                 dn->bv_val, res->bv_val, 0 );
173 #else
174                         Debug( LDAP_DEBUG_ARGS,
175                                 "ldap_back_dn_massage:"
176                                 " converted \"%s\" to \"%s\"\n",
177                                 dn->bv_val, res->bv_val, 0 );
178 #endif
179                         break;
180                 }
181         }
182         if (pretty.bv_val) {
183                 ch_free(pretty.bv_val);
184                 dn = odn;
185         }
186         /* Nothing matched, just return the original DN */
187         if (res->bv_val == NULL) {
188                 *res = *dn;
189         }
190
191         return 0;
192 }
193 #endif /* !ENABLE_REWRITE */