]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/suffixmassage.c
fix for select_backend suggested G. Gombas (ITS 1090)
[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 /*
41  * ldap_back_dn_massage
42  * 
43  * Aliases the suffix; based on suffix_alias (servers/slapd/suffixalias.c).
44  */
45 char *
46 ldap_back_dn_massage(
47         struct ldapinfo *li,
48         char *dn,
49         int normalized
50 )
51 {
52         int     i, dnLength;
53
54         if ( dn == NULL ) {
55                 return NULL;
56         }
57         if ( li == NULL ) {
58                 return dn;
59         }
60
61         dnLength = strlen ( dn );
62
63         for ( i = 0;
64                 li->suffix_massage != NULL && li->suffix_massage[i] != NULL;
65                 i += 4 ) {
66                 int aliasLength = strlen( li->suffix_massage[i+normalized] );
67                 int diff = dnLength - aliasLength;
68
69                 if ( diff < 0 ) {
70                         /* alias is longer than dn */
71                         continue;
72                                                                                                 } else if ( diff > 0 ) {
73                         if ( normalized && ( ! DN_SEPARATOR(dn[diff-1]) ) ) {
74                                 /* boundary is not at a DN separator */
75                                 continue;
76                         }
77                         /* At a DN Separator */
78                         /* XXX or an escaped separator... oh well */
79                 }
80
81                 if ( !strcmp( li->suffix_massage[i+normalized], &dn[diff] ) ) {
82                         char *oldDN = dn;
83                         dn = ch_malloc( diff + strlen( li->suffix_massage[i+2+normalized] ) + 1 );
84                         strncpy( dn, oldDN, diff );
85                         strcpy( &dn[diff], li->suffix_massage[i+2+normalized] );
86                         Debug( LDAP_DEBUG_ARGS,
87                                 "ldap_back_dn_massage:"
88                                 " converted \"%s\" to \"%s\"\n",
89                                 oldDN, dn, 0 );
90                         free( oldDN );
91                         break;
92                 }
93         }
94
95         return dn;
96 }
97
98 /*
99  * ldap_back_dn_restore
100  * 
101  * Restores the original suffix;
102  * based on suffix_alias (servers/slapd/suffixalias.c).
103  */
104 char *
105 ldap_back_dn_restore(
106         struct ldapinfo *li,
107         char *dn,
108         int normalized
109         )
110 {
111         int     i, dnLength;
112
113         if ( dn == NULL ) {
114                 return NULL;
115         }
116         if ( li == NULL ) {
117                 return dn;
118         }
119
120         dnLength = strlen ( dn );
121
122         for ( i = 0;
123                 li->suffix_massage != NULL && li->suffix_massage[i] != NULL;
124                 i += 4 ) {
125                 int aliasLength = strlen( li->suffix_massage[i+2+normalized] );
126                 int diff = dnLength - aliasLength;
127
128                 if ( diff < 0 ) {
129                         /* alias is longer than dn */
130                         continue;
131
132                 } else if ( diff > 0 ) {
133                         if ( normalized && ( ! DN_SEPARATOR(dn[diff-1]) ) ) {
134                                 /* boundary is not at a DN separator */
135                                 continue;
136                         }
137                         /* At a DN Separator */
138                         /* XXX or an escaped separator... oh well */
139                 }
140
141                 if ( !strcmp( li->suffix_massage[i+2+normalized], &dn[diff] ) ) {
142                         char *oldDN = dn;
143                         dn = ch_malloc( diff + strlen( li->suffix_massage[i+normalized] ) + 1 );
144                         strncpy( dn, oldDN, diff );
145                         strcpy( &dn[diff], li->suffix_massage[i+normalized] );
146                         Debug( LDAP_DEBUG_ARGS,
147                                 "ldap_back_dn_restore:"
148                                 " converted \"%s\" to \"%s\"\n",
149                                 oldDN, dn, 0 );
150                         free( oldDN );
151                         break;
152                 }
153         }
154
155         return dn;
156 }
157