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