]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Added mixed-case as well as up-cased DN argument. The behavior of back-bdb2
[openldap] / servers / slapd / modrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 /*
19  * LDAP v3 newSuperior support.
20  *
21  * Copyright 1999, Juan C. Gomez, All rights reserved.
22  * This software is not subject to any license of Silicon Graphics 
23  * Inc. or Purdue University.
24  *
25  * Redistribution and use in source and binary forms are permitted
26  * without restriction or fee of any kind as long as this notice
27  * is preserved.
28  *
29  */
30
31 #include "portable.h"
32
33 #include <stdio.h>
34
35 #include <ac/socket.h>
36 #include <ac/string.h>
37
38 #include "slap.h"
39
40 int
41 do_modrdn(
42     Connection  *conn,
43     Operation   *op
44 )
45 {
46         char    *dn, *ndn, *newrdn;
47         ber_int_t       deloldrdn;
48         Backend *be;
49         /* Vars for LDAP v3 newSuperior support */
50         char    *newSuperior = NULL;
51         char    *nnewSuperior = NULL;
52         Backend *newSuperior_be = NULL;
53         ber_len_t       length;
54         int rc;
55
56         Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
57
58         if( op->o_bind_in_progress ) {
59                 Debug( LDAP_DEBUG_ANY, "do_modrdn: SASL bind in progress.\n",
60                         0, 0, 0 );
61                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
62                         NULL, "SASL bind in progress", NULL, NULL );
63                 return LDAP_SASL_BIND_IN_PROGRESS;
64         }
65
66         /*
67          * Parse the modrdn request.  It looks like this:
68          *
69          *      ModifyRDNRequest := SEQUENCE {
70          *              entry   DistinguishedName,
71          *              newrdn  RelativeDistinguishedName
72          *              deleteoldrdn    BOOLEAN,
73          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
74          *      }
75          */
76
77         if ( ber_scanf( op->o_ber, "{aab", &dn, &newrdn, &deloldrdn )
78             == LBER_ERROR ) {
79                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
80                 send_ldap_disconnect( conn, op,
81                         LDAP_PROTOCOL_ERROR, "decoding error" );
82                 return -1;
83         }
84
85         if( dn_normalize( dn ) == NULL ) {
86                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid dn (%s)\n", dn, 0, 0 );
87                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
88                     "invalid DN", NULL, NULL );
89                 free( dn );
90                 free( newrdn );
91                 return rc;
92         }
93
94         if( !rdn_validate( newrdn ) ) {
95                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n", newrdn, 0, 0 );
96                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
97                     "invalid RDN", NULL, NULL );
98                 free( dn );
99                 free( newrdn );
100                 return rc;
101         }
102
103         /* Check for newSuperior parameter, if present scan it */
104
105         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
106                 if ( op->o_protocol < LDAP_VERSION3 ) {
107                         /* Conection record indicates v2 but field 
108                          * newSuperior is present: report error.
109                          */
110                         Debug( LDAP_DEBUG_ANY,
111                                "modrdn(v2): invalid field newSuperior!\n",
112                                0, 0, 0 );
113                         send_ldap_disconnect( conn, op,
114                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
115                         free( dn );
116                         free( newrdn );
117                         return -1;
118                 }
119
120                 if ( ber_scanf( op->o_ber, "a", &newSuperior ) 
121                      == LBER_ERROR ) {
122
123                     Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\") failed\n",
124                            0, 0, 0 );
125                         send_ldap_disconnect( conn, op,
126                                 LDAP_PROTOCOL_ERROR, "decoding error" );
127                         free( dn );
128                         free( newrdn );
129                     return -1;
130                 }
131
132                 nnewSuperior = ch_strdup( newSuperior );
133
134                 if( dn_normalize_case( nnewSuperior ) == NULL ) {
135                         Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid new superior (%s)\n",
136                                 newSuperior, 0, 0 );
137                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
138                                 "invalid (new superior) DN", NULL, NULL );
139                         goto done;
140                 }
141
142         }
143
144         Debug( LDAP_DEBUG_ARGS,
145             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
146                 dn, newrdn,
147                 newSuperior != NULL ? newSuperior : "" );
148
149         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
150                 free( dn );
151                 free( newrdn ); 
152                 free( newSuperior );
153                 free( nnewSuperior );
154                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
155                 send_ldap_disconnect( conn, op,
156                                 LDAP_PROTOCOL_ERROR, "decoding error" );
157                 return -1;
158         }
159
160         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
161                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
162                 /* get_ctrls has sent results.  Now clean up. */
163                 goto done;
164         } 
165
166         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
167             op->o_connid, op->o_opid, dn, 0, 0 );
168
169         /*
170          * We could be serving multiple database backends.  Select the
171          * appropriate one, or send a referral to our "referral server"
172          * if we don't hold it.
173          */
174
175         ndn = ch_strdup( dn );
176         ldap_pvt_str2upper( ndn );
177
178         if ( (be = select_backend( ndn )) == NULL ) {
179                 free( dn );
180                 free( ndn );
181                 free( newrdn ); 
182                 free( newSuperior );
183                 free( nnewSuperior );
184                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
185                         NULL, NULL, default_referral, NULL );
186                 return rc;
187         }
188
189         if ( global_readonly || be->be_readonly ) {
190                 Debug( LDAP_DEBUG_ANY, "do_modrdn: database is read-only\n",
191                        0, 0, 0 );
192                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
193                                   NULL, "database is read-only", NULL, NULL );
194                 goto done;
195         }
196
197         /* Make sure that the entry being changed and the newSuperior are in 
198          * the same backend, otherwise we return an error.
199          */
200         if( newSuperior != NULL ) {
201                 newSuperior_be = select_backend( nnewSuperior );
202
203                 if ( newSuperior_be != be ) {
204                         /* newSuperior is in same backend */
205                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
206
207                         send_ldap_result( conn, op, rc,
208                                 NULL, NULL, NULL, NULL );
209
210                         free( dn );
211                         free( ndn );
212                         free( newrdn );
213                         free( newSuperior );
214                         free( nnewSuperior );
215
216                         return rc;
217                 }
218
219                 /* deref suffix alias if appropriate */
220                 nnewSuperior = suffix_alias( be, nnewSuperior );
221         }
222
223         /* deref suffix alias if appropriate */
224         ndn = suffix_alias( be, ndn );
225
226         /*
227          * do the add if 1 && (2 || 3)
228          * 1) there is an add function implemented in this backend;
229          * 2) this backend is master for what it holds;
230          * 3) it's a replica and the dn supplied is the update_ndn.
231          */
232         if ( be->be_modrdn ) {
233                 /* do the update here */
234 #ifndef SLAPD_MULTIMASTER
235                 if ( be->be_update_ndn == NULL ||
236                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
237 #endif
238                 {
239                         if ( (*be->be_modrdn)( be, conn, op, dn, ndn, newrdn,
240                             deloldrdn, newSuperior ) == 0
241 #ifdef SLAPD_MULTIMASTER
242                                 && ( be->be_update_ndn == NULL ||
243                                         strcmp( be->be_update_ndn, op->o_ndn ) )
244 #endif
245                         ) {
246                                 struct replog_moddn moddn;
247                                 moddn.newrdn = newrdn;
248                                 moddn.deloldrdn = deloldrdn;
249                                 moddn.newsup = newSuperior;
250
251                                 replog( be, op, dn, &moddn );
252                         }
253 #ifndef SLAPD_MULTIMASTER
254                 } else {
255                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
256                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
257 #endif
258                 }
259         } else {
260                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
261                         NULL, "Function not implemented", NULL, NULL );
262         }
263
264 done:
265         free( dn );
266         free( ndn );
267         free( newrdn ); 
268         free( newSuperior );
269         free( nnewSuperior );
270         return rc;
271 }