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