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