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