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