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