]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/suffixmassage.c
Happy new year
[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 void
48 ldap_back_dn_massage(
49         struct ldapinfo *li,
50         struct berval *dn,
51         struct berval *res,
52         int normalized,
53         int tofrom
54 )
55 {
56         int     i, src, dst;
57
58         assert( res );
59
60         if ( dn == NULL ) {
61                 res->bv_val = NULL;
62                 res->bv_len = 0;
63                 return;
64         }
65         if ( li == NULL || li->suffix_massage == NULL ) {
66                 *res = *dn;
67                 return;
68         }
69
70         if ( tofrom ) {
71                 src = 0 + normalized;
72                 dst = 2 + normalized;
73         } else {
74                 src = 2 + normalized;
75                 dst = 0 + normalized;
76         }
77
78         for ( i = 0;
79                 li->suffix_massage[i].bv_val != NULL;
80                 i += 4 ) {
81                 int aliasLength = li->suffix_massage[i+src].bv_len;
82                 int diff = dn->bv_len - aliasLength;
83
84                 if ( diff < 0 ) {
85                         /* alias is longer than dn */
86                         continue;
87                                                                                                 } else if ( diff > 0 ) {
88                         if ( normalized && ( ! DN_SEPARATOR(dn->bv_val[diff-1]) ) ) {
89                                 /* boundary is not at a DN separator */
90                                 continue;
91                         }
92                         /* At a DN Separator */
93                         /* XXX or an escaped separator... oh well */
94                 }
95
96                 if ( !strcmp( li->suffix_massage[i+src].bv_val, &dn->bv_val[diff] ) ) {
97                         res->bv_len = diff + li->suffix_massage[i+dst].bv_len;
98                         res->bv_val = ch_malloc( res->bv_len + 1 );
99                         strncpy( res->bv_val, dn->bv_val, diff );
100                         strcpy( &res->bv_val[diff], li->suffix_massage[i+dst].bv_val );
101 #ifdef NEW_LOGGING
102                                         LDAP_LOG ( BACK_LDAP, ARGS, 
103                                                 "ldap_back_dn_massage: converted \"%s\" to \"%s\"\n",
104                                                 dn->bv_val, res->bv_val, 0 );
105 #else
106                         Debug( LDAP_DEBUG_ARGS,
107                                 "ldap_back_dn_massage:"
108                                 " converted \"%s\" to \"%s\"\n",
109                                 dn->bv_val, res->bv_val, 0 );
110 #endif
111                         break;
112                 }
113         }
114
115         return;
116 }
117 #endif /* !ENABLE_REWRITE */