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