]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Rework BER decoding with lieu of LDAPv3 controls (coming soon).
[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 void
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
50         Debug( LDAP_DEBUG_TRACE, "do_modrdn\n", 0, 0, 0 );
51
52         /*
53          * Parse the modrdn request.  It looks like this:
54          *
55          *      ModifyRDNRequest := SEQUENCE {
56          *              entry   DistinguishedName,
57          *              newrdn  RelativeDistinguishedName
58          *              deleteoldrdn    BOOLEAN,
59          *              newSuperior     [0] LDAPDN OPTIONAL (v3 Only!)
60          *      }
61          */
62
63         if ( ber_scanf( op->o_ber, "{aab", &ndn, &newrdn, &deloldrdn )
64             == LBER_ERROR ) {
65                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
66                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
67                 return;
68         }
69
70         /* Check for newSuperior parameter, if present scan it */
71
72         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
73
74                 if ( op->o_protocol ==  0 ) {
75                         /*
76                          * Promote to LDAPv3
77                          */
78                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
79                         conn->c_protocol = LDAP_VERSION3;
80                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
81                         op->o_protocol = LDAP_VERSION3;
82
83                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
84                         /* Conection record indicates v2 but field 
85                          * newSuperior is present: report error.
86                          */
87                         Debug( LDAP_DEBUG_ANY,
88                                "modrdn(v2): invalid field newSuperior!\n",
89                                0, 0, 0 );
90                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
91                                           NULL, "" );
92                         return;
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_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
101                                       "" );
102                     return;
103
104                 }
105
106         }
107
108         Debug( LDAP_DEBUG_ARGS,
109             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
110                 ndn, newrdn,
111                 newSuperior != NULL ? newSuperior : "" );
112
113         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
114                 free( ndn );
115                 free( newrdn ); 
116                 free( newSuperior );
117                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
118                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
119                     "decoding error" );
120                 return;
121         }
122
123 #ifdef  GET_CTRLS
124         if( get_ctrls( conn, op, 1 ) == -1 ) {
125                 free( ndn );
126                 free( newrdn ); 
127                 free( newSuperior );
128                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
129                 return;
130         } 
131 #endif
132
133         if( newSuperior != NULL ) {
134                 /* GET BACKEND FOR NEW SUPERIOR */
135
136                 nnewSuperior = strdup( newSuperior );
137                 dn_normalize_case( nnewSuperior );
138
139                 if ( (newSuperior_be = select_backend( nnewSuperior )) 
140                      == NULL ) {
141                     
142                         /* We do not have a backend for newSuperior so we send
143                          * a referral.
144                          * XXX: We may need to do something else here, not sure
145                          * what though.
146                          */
147
148                         Debug( LDAP_DEBUG_ARGS,
149                                "do_modrdn: cant find backend for=(%s)\n",
150                                newSuperior, 0, 0 );
151                         
152                         free( ndn );
153                         free( newrdn );
154                         free( newSuperior );
155                         free( nnewSuperior );
156                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
157                                           default_referral );
158                         return;
159                 }
160         }
161
162         dn_normalize_case( ndn );
163
164         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MODRDN dn=\"%s\"\n",
165             conn->c_connid, op->o_opid, ndn, 0, 0 );
166
167         /*
168          * We could be serving multiple database backends.  Select the
169          * appropriate one, or send a referral to our "referral server"
170          * if we don't hold it.
171          */
172
173         if ( (be = select_backend( ndn )) == NULL ) {
174                 free( ndn );
175                 free( newrdn ); 
176                 free( newSuperior );
177                 free( nnewSuperior );
178                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
179                     default_referral );
180                 return;
181         }
182
183         /* Make sure that the entry being changed and the newSuperior are in 
184          * the same backend, otherwise we return an error.
185          */
186
187         if ( (newSuperior_be != NULL) && ( be != newSuperior_be) ) {
188
189                 Debug( LDAP_DEBUG_ANY, "dn=(%s), newSuperior=(%s)\n", ndn,
190                        newSuperior, 0 );
191                 
192                 free( ndn );
193                 free( newrdn );
194                 free( newSuperior );
195                 free( nnewSuperior );
196                 
197                 send_ldap_result( conn, op, LDAP_AFFECTS_MULTIPLE_DSAS,
198                                   NULL, "" );
199             
200                 return;
201
202         }
203
204
205         /* alias suffix if approp */
206         ndn = suffixAlias( ndn, op, be );
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                 if ( be->be_update_ndn == NULL ||
217                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
218                 {
219                         if ( (*be->be_modrdn)( be, conn, op, ndn, newrdn,
220                             deloldrdn, newSuperior ) == 0 ) {
221                                 /* XXX: MAY NEEED TO ADD newSuperior HERE */
222                                 replog( be, LDAP_REQ_MODRDN, ndn, newrdn,
223                                     deloldrdn );
224                         }
225                 } else {
226                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
227                             default_referral );
228                 }
229         } else {
230                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
231                     "Function not implemented" );
232         }
233
234         free( ndn );
235         free( newrdn ); 
236         free( newSuperior );
237         free( nnewSuperior );
238 }