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