3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
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.
19 * LDAP v3 newSuperior support.
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.
25 * Redistribution and use in source and binary forms are permitted
26 * without restriction or fee of any kind as long as this notice
35 #include <ac/socket.h>
36 #include <ac/string.h>
47 char *dn, *ndn, *newrdn;
50 /* Vars for LDAP v3 newSuperior support */
51 char *newSuperior = NULL;
52 char *nnewSuperior = NULL;
53 Backend *newSuperior_be = NULL;
57 Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
60 * Parse the modrdn request. It looks like this:
62 * ModifyRDNRequest := SEQUENCE {
63 * entry DistinguishedName,
64 * newrdn RelativeDistinguishedName
65 * deleteoldrdn BOOLEAN,
66 * newSuperior [0] LDAPDN OPTIONAL (v3 Only!)
70 if ( ber_scanf( op->o_ber, "{aab", &dn, &newrdn, &deloldrdn )
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;
78 /* Check for newSuperior parameter, if present scan it */
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.
85 Debug( LDAP_DEBUG_ANY,
86 "modrdn(v2): invalid field newSuperior!\n",
88 send_ldap_disconnect( conn, op,
89 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
90 rc = SLAPD_DISCONNECT;
94 if ( ber_scanf( op->o_ber, "a", &newSuperior )
97 Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\") failed\n",
99 send_ldap_disconnect( conn, op,
100 LDAP_PROTOCOL_ERROR, "decoding error" );
101 rc = SLAPD_DISCONNECT;
105 nnewSuperior = ch_strdup( newSuperior );
107 if( dn_normalize( nnewSuperior ) == NULL ) {
108 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid new superior (%s)\n",
110 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
111 "invalid (new superior) DN", NULL, NULL );
117 Debug( LDAP_DEBUG_ARGS,
118 "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
120 newSuperior != NULL ? newSuperior : "" );
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;
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. */
136 ndn = ch_strdup( dn );
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 );
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 );
152 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MODRDN dn=\"%s\"\n",
153 op->o_connid, op->o_opid, dn, 0, 0 );
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.
161 if ( (be = select_backend( ndn )) == NULL ) {
162 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
163 NULL, NULL, default_referral, NULL );
167 /* make sure this backend recongizes critical controls */
168 rc = backend_check_controls( be, conn, op ) ;
170 if( rc != LDAP_SUCCESS ) {
171 send_ldap_result( conn, op, rc,
172 NULL, NULL, NULL, NULL );
176 if ( global_readonly || be->be_readonly ) {
177 Debug( LDAP_DEBUG_ANY, "do_modrdn: database is read-only\n",
179 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
180 NULL, "database is read-only", NULL, NULL );
184 /* Make sure that the entry being changed and the newSuperior are in
185 * the same backend, otherwise we return an error.
187 if( newSuperior != NULL ) {
188 newSuperior_be = select_backend( nnewSuperior );
190 if ( newSuperior_be != be ) {
191 /* newSuperior is in same backend */
192 rc = LDAP_AFFECTS_MULTIPLE_DSAS;
194 send_ldap_result( conn, op, rc,
195 NULL, NULL, NULL, NULL );
200 /* deref suffix alias if appropriate */
201 nnewSuperior = suffix_alias( be, nnewSuperior );
204 /* deref suffix alias if appropriate */
205 ndn = suffix_alias( be, ndn );
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.
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 )
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 ) )
227 struct replog_moddn moddn;
228 moddn.newrdn = newrdn;
229 moddn.deloldrdn = deloldrdn;
230 moddn.newsup = newSuperior;
232 replog( be, op, dn, &moddn );
234 #ifndef SLAPD_MULTIMASTER
236 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
237 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
241 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
242 NULL, "Function not implemented", NULL, NULL );
249 if ( newSuperior != NULL )
251 if ( nnewSuperior != NULL )
252 free( nnewSuperior );