]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
78b56666d5f9ff10274be16009b057dc1b5ba6dc
[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 "ldap_pvt.h"
39 #include "slap.h"
40
41 int
42 do_modrdn(
43     Connection  *conn,
44     Operation   *op
45 )
46 {
47         char    *dn, *ndn, *newrdn;
48         ber_int_t       deloldrdn;
49         Backend *be;
50         /* Vars for LDAP v3 newSuperior support */
51         char    *newSuperior = NULL;
52         char    *nnewSuperior = NULL;
53         Backend *newSuperior_be = NULL;
54         ber_len_t       length;
55         int rc;
56
57         Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
58
59         if( op->o_bind_in_progress ) {
60                 Debug( LDAP_DEBUG_ANY, "do_modrdn: SASL bind in progress.\n",
61                         0, 0, 0 );
62                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
63                         NULL, "SASL bind in progress", NULL, NULL );
64                 return LDAP_SASL_BIND_IN_PROGRESS;
65         }
66
67         /*
68          * Parse the modrdn request.  It looks like this:
69          *
70          *      ModifyRDNRequest := SEQUENCE {
71          *              entry   DistinguishedName,
72          *              newrdn  RelativeDistinguishedName
73          *              deleteoldrdn    BOOLEAN,
74          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
75          *      }
76          */
77
78         if ( ber_scanf( op->o_ber, "{aab", &dn, &newrdn, &deloldrdn )
79             == LBER_ERROR ) {
80                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
81                 send_ldap_disconnect( conn, op,
82                         LDAP_PROTOCOL_ERROR, "decoding error" );
83                 return SLAPD_DISCONNECT;
84         }
85
86         /* Check for newSuperior parameter, if present scan it */
87
88         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
89                 if ( op->o_protocol < LDAP_VERSION3 ) {
90                         /* Conection record indicates v2 but field 
91                          * newSuperior is present: report error.
92                          */
93                         Debug( LDAP_DEBUG_ANY,
94                                "modrdn(v2): invalid field newSuperior!\n",
95                                0, 0, 0 );
96                         send_ldap_disconnect( conn, op,
97                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
98                         rc = SLAPD_DISCONNECT;
99                         goto cleanup;
100                 }
101
102                 if ( ber_scanf( op->o_ber, "a", &newSuperior ) 
103                      == LBER_ERROR ) {
104
105                         Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\") failed\n",
106                            0, 0, 0 );
107                         send_ldap_disconnect( conn, op,
108                                 LDAP_PROTOCOL_ERROR, "decoding error" );
109                         rc = SLAPD_DISCONNECT;
110                         goto cleanup;
111                 }
112
113                 nnewSuperior = ch_strdup( newSuperior );
114
115                 if( dn_normalize( nnewSuperior ) == NULL ) {
116                         Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid new superior (%s)\n",
117                                 newSuperior, 0, 0 );
118                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
119                                 "invalid (new superior) DN", NULL, NULL );
120                         goto cleanup;
121                 }
122
123         }
124
125         Debug( LDAP_DEBUG_ARGS,
126             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
127                 dn, newrdn,
128                 newSuperior != NULL ? newSuperior : "" );
129
130         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
131                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
132                 send_ldap_disconnect( conn, op,
133                                 LDAP_PROTOCOL_ERROR, "decoding error" );
134                 rc = SLAPD_DISCONNECT;
135                 goto cleanup;
136         }
137
138         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
139                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
140                 /* get_ctrls has sent results.  Now clean up. */
141                 goto cleanup;
142         } 
143
144         ndn = ch_strdup( dn );
145
146         if( dn_normalize( ndn ) == NULL ) {
147                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid dn (%s)\n", dn, 0, 0 );
148                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
149                     "invalid DN", NULL, NULL );
150                 goto cleanup;
151         }
152
153         if( !rdn_validate( newrdn ) ) {
154                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n", newrdn, 0, 0 );
155                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
156                     "invalid RDN", NULL, NULL );
157                 goto cleanup;
158         }
159
160         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
161             op->o_connid, op->o_opid, dn, 0, 0 );
162
163         /*
164          * We could be serving multiple database backends.  Select the
165          * appropriate one, or send a referral to our "referral server"
166          * if we don't hold it.
167          */
168
169         if ( (be = select_backend( ndn )) == NULL ) {
170                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
171                         NULL, NULL, default_referral, NULL );
172                 goto cleanup;
173         }
174
175         /* make sure this backend recongizes critical controls */
176         rc = backend_check_controls( be, conn, op ) ;
177
178         if( rc != LDAP_SUCCESS ) {
179                 send_ldap_result( conn, op, rc,
180                         NULL, NULL, NULL, NULL );
181                 goto cleanup;
182         }
183
184         if ( global_readonly || be->be_readonly ) {
185                 Debug( LDAP_DEBUG_ANY, "do_modrdn: database is read-only\n",
186                        0, 0, 0 );
187                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
188                                   NULL, "database is read-only", NULL, NULL );
189                 goto cleanup;
190         }
191
192         /* Make sure that the entry being changed and the newSuperior are in 
193          * the same backend, otherwise we return an error.
194          */
195         if( newSuperior != NULL ) {
196                 newSuperior_be = select_backend( nnewSuperior );
197
198                 if ( newSuperior_be != be ) {
199                         /* newSuperior is in same backend */
200                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
201
202                         send_ldap_result( conn, op, rc,
203                                 NULL, NULL, NULL, NULL );
204
205                         goto cleanup;
206                 }
207
208                 /* deref suffix alias if appropriate */
209                 nnewSuperior = suffix_alias( be, nnewSuperior );
210         }
211
212         /* deref suffix alias if appropriate */
213         ndn = suffix_alias( be, ndn );
214
215         /*
216          * do the add if 1 && (2 || 3)
217          * 1) there is an add function implemented in this backend;
218          * 2) this backend is master for what it holds;
219          * 3) it's a replica and the dn supplied is the update_ndn.
220          */
221         if ( be->be_modrdn ) {
222                 /* do the update here */
223 #ifndef SLAPD_MULTIMASTER
224                 if ( be->be_update_ndn == NULL ||
225                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
226 #endif
227                 {
228                         if ( (*be->be_modrdn)( be, conn, op, dn, ndn, newrdn,
229                             deloldrdn, newSuperior ) == 0
230 #ifdef SLAPD_MULTIMASTER
231                                 && ( be->be_update_ndn == NULL ||
232                                         strcmp( be->be_update_ndn, op->o_ndn ) )
233 #endif
234                         ) {
235                                 struct replog_moddn moddn;
236                                 moddn.newrdn = newrdn;
237                                 moddn.deloldrdn = deloldrdn;
238                                 moddn.newsup = newSuperior;
239
240                                 replog( be, op, dn, &moddn );
241                         }
242 #ifndef SLAPD_MULTIMASTER
243                 } else {
244                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
245                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
246 #endif
247                 }
248         } else {
249                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
250                         NULL, "Function not implemented", NULL, NULL );
251         }
252
253 cleanup:
254         free( dn );
255         free( ndn );
256         free( newrdn ); 
257         if ( newSuperior != NULL )
258                 free( newSuperior );
259         if ( nnewSuperior != NULL )
260                 free( nnewSuperior );
261         return rc;
262 }