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