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