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