]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Send LDAP_SASL_BIND_IN_PROGRESS if o_bind_in_progress is true.
[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, NULL,
57                     "SASL bind in progress" );
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_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL, "" );
76                 return rc;
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
83                 if ( op->o_protocol == 0 ) {
84                         /*
85                          * Promote to LDAPv3
86                          */
87                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
88                         conn->c_protocol = LDAP_VERSION3;
89                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
90                         op->o_protocol = LDAP_VERSION3;
91
92                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
93                         /* Conection record indicates v2 but field 
94                          * newSuperior is present: report error.
95                          */
96                         Debug( LDAP_DEBUG_ANY,
97                                "modrdn(v2): invalid field newSuperior!\n",
98                                0, 0, 0 );
99                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
100                                           NULL, "" );
101                         return rc;
102                 }
103
104                 if ( ber_scanf( op->o_ber, "a", &newSuperior ) 
105                      == LBER_ERROR ) {
106
107                     Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\"}) failed\n",
108                            0, 0, 0 );
109                     send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
110                                       "" );
111                     return rc;
112
113                 }
114
115         }
116
117         Debug( LDAP_DEBUG_ARGS,
118             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
119                 ndn, newrdn,
120                 newSuperior != NULL ? newSuperior : "" );
121
122         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
123                 free( ndn );
124                 free( newrdn ); 
125                 free( newSuperior );
126                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
127                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR, NULL,
128                     "decoding error" );
129                 return rc;
130         }
131
132 #ifdef  GET_CTRLS
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 #endif
141
142         if( newSuperior != NULL ) {
143                 /* GET BACKEND FOR NEW SUPERIOR */
144
145                 nnewSuperior = strdup( newSuperior );
146                 dn_normalize_case( nnewSuperior );
147
148                 if ( (newSuperior_be = select_backend( nnewSuperior )) 
149                      == NULL ) {
150                     
151                         /* We do not have a backend for newSuperior so we send
152                          * a referral.
153                          * XXX: We may need to do something else here, not sure
154                          * what though.
155                          */
156
157                         Debug( LDAP_DEBUG_ARGS,
158                                "do_modrdn: cant find backend for=(%s)\n",
159                                newSuperior, 0, 0 );
160                         
161                         free( ndn );
162                         free( newrdn );
163                         free( newSuperior );
164                         free( nnewSuperior );
165                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
166                                           default_referral );
167                         return 0;
168                 }
169         }
170
171         dn_normalize_case( ndn );
172
173         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MODRDN dn=\"%s\"\n",
174             conn->c_connid, op->o_opid, ndn, 0, 0 );
175
176         /*
177          * We could be serving multiple database backends.  Select the
178          * appropriate one, or send a referral to our "referral server"
179          * if we don't hold it.
180          */
181
182         if ( (be = select_backend( ndn )) == NULL ) {
183                 free( ndn );
184                 free( newrdn ); 
185                 free( newSuperior );
186                 free( nnewSuperior );
187                 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
188                     default_referral );
189                 return rc;
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
196         if ( (newSuperior_be != NULL) && ( be != newSuperior_be) ) {
197
198                 Debug( LDAP_DEBUG_ANY, "dn=(%s), newSuperior=(%s)\n", ndn,
199                        newSuperior, 0 );
200                 
201                 free( ndn );
202                 free( newrdn );
203                 free( newSuperior );
204                 free( nnewSuperior );
205                 
206                 send_ldap_result( conn, op, rc = LDAP_AFFECTS_MULTIPLE_DSAS,
207                                   NULL, "" );
208             
209                 return rc;
210
211         }
212
213
214         /* alias suffix if approp */
215         ndn = suffixAlias( ndn, op, be );
216
217         /*
218          * do the add if 1 && (2 || 3)
219          * 1) there is an add function implemented in this backend;
220          * 2) this backend is master for what it holds;
221          * 3) it's a replica and the dn supplied is the update_ndn.
222          */
223         if ( be->be_modrdn ) {
224                 /* do the update here */
225                 if ( be->be_update_ndn == NULL ||
226                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
227                 {
228                         if ( (*be->be_modrdn)( be, conn, op, ndn, newrdn,
229                             deloldrdn, newSuperior ) == 0 ) {
230                                 /* XXX: MAY NEED TO ADD newSuperior HERE */
231                                 replog( be, LDAP_REQ_MODRDN, ndn, newrdn,
232                                     deloldrdn );
233                         }
234                 } else {
235                         send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS, NULL,
236                             default_referral );
237                 }
238         } else {
239                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
240                     "Function not implemented" );
241         }
242
243         free( ndn );
244         free( newrdn ); 
245         free( newSuperior );
246         free( nnewSuperior );
247         return rc;
248 }