]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Change 'unsigned long len' to ber_len_t in get_filter()
[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         if( dn_normalize_case( ndn ) == NULL ) {
81                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid dn (%s)\n", ndn, 0, 0 );
82                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
83                     "invalid DN", NULL, NULL );
84                 free( ndn );
85                 free( newrdn );
86                 return rc;
87         }
88
89         if( !rdn_validate( newrdn ) ) {
90                 Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid rdn (%s)\n", newrdn, 0, 0 );
91                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
92                     "invalid RDN", NULL, NULL );
93                 free( ndn );
94                 free( newrdn );
95                 return rc;
96         }
97
98         /* Check for newSuperior parameter, if present scan it */
99
100         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
101
102                 if ( op->o_protocol == 0 ) {
103                         /*
104                          * Promote to LDAPv3
105                          */
106                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
107                         conn->c_protocol = LDAP_VERSION3;
108                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
109                         op->o_protocol = LDAP_VERSION3;
110
111                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
112                         /* Conection record indicates v2 but field 
113                          * newSuperior is present: report error.
114                          */
115                         Debug( LDAP_DEBUG_ANY,
116                                "modrdn(v2): invalid field newSuperior!\n",
117                                0, 0, 0 );
118                         send_ldap_disconnect( conn, op,
119                                 LDAP_PROTOCOL_ERROR, "newSuperior requires LDAPv3" );
120                         return -1;
121                 }
122
123                 if ( ber_scanf( op->o_ber, "a", &newSuperior ) 
124                      == LBER_ERROR ) {
125
126                     Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\"}) failed\n",
127                            0, 0, 0 );
128                         send_ldap_disconnect( conn, op,
129                                 LDAP_PROTOCOL_ERROR, "decoding error" );
130                     return -1;
131                 }
132
133                 nnewSuperior = ch_strdup( newSuperior );
134
135                 if( dn_normalize_case( nnewSuperior ) == NULL ) {
136                         Debug( LDAP_DEBUG_ANY, "do_modrdn: invalid new superior (%s)\n",
137                                 newSuperior, 0, 0 );
138                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
139                                 "invalid (new superior) DN", NULL, NULL );
140                         free( ndn );
141                         free( newrdn );
142                         return rc;
143                 }
144
145         }
146
147         Debug( LDAP_DEBUG_ARGS,
148             "do_modrdn: dn (%s) newrdn (%s) newsuperior (%s)\n",
149                 ndn, newrdn,
150                 newSuperior != NULL ? newSuperior : "" );
151
152         if ( ber_scanf( op->o_ber, /*{*/ "}") == LBER_ERROR ) {
153                 free( ndn );
154                 free( newrdn ); 
155                 free( newSuperior );
156                 free( nnewSuperior );
157                 Debug( LDAP_DEBUG_ANY, "do_modrdn: ber_scanf failed\n", 0, 0, 0 );
158                 send_ldap_disconnect( conn, op,
159                                 LDAP_PROTOCOL_ERROR, "decoding error" );
160                 return -1;
161         }
162
163         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
164                 free( ndn );
165                 free( newrdn ); 
166                 free( newSuperior );
167                 Debug( LDAP_DEBUG_ANY, "do_modrdn: get_ctrls failed\n", 0, 0, 0 );
168                 return rc;
169         } 
170
171         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MODRDN dn=\"%s\"\n",
172             op->o_connid, op->o_opid, ndn, 0, 0 );
173
174         /*
175          * We could be serving multiple database backends.  Select the
176          * appropriate one, or send a referral to our "referral server"
177          * if we don't hold it.
178          */
179
180         if ( (be = select_backend( ndn )) == NULL ) {
181                 free( ndn );
182                 free( newrdn ); 
183                 free( newSuperior );
184                 free( nnewSuperior );
185                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
186                         NULL, NULL, default_referral, NULL );
187                 return rc;
188         }
189
190         /* Make sure that the entry being changed and the newSuperior are in 
191          * the same backend, otherwise we return an error.
192          */
193         if( newSuperior != NULL ) {
194                 newSuperior_be = select_backend( nnewSuperior );
195
196                 if ( newSuperior_be != be ) {
197                         /* newSuperior is in same backend */
198                         rc = LDAP_AFFECTS_MULTIPLE_DSAS;
199
200                         send_ldap_result( conn, op, rc,
201                                 NULL, NULL, NULL, NULL );
202
203                         free( ndn );
204                         free( newrdn );
205                         free( newSuperior );
206                         free( nnewSuperior );
207
208                         return rc;
209                 }
210
211                 /* deref suffix alias if appropriate */
212                 nnewSuperior = suffix_alias( be, nnewSuperior );
213         }
214
215         /* deref suffix alias if appropriate */
216         ndn = suffix_alias( be, ndn );
217
218         /*
219          * do the add if 1 && (2 || 3)
220          * 1) there is an add function implemented in this backend;
221          * 2) this backend is master for what it holds;
222          * 3) it's a replica and the dn supplied is the update_ndn.
223          */
224         if ( be->be_modrdn ) {
225                 /* do the update here */
226                 if ( be->be_update_ndn == NULL ||
227                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
228                 {
229                         if ( (*be->be_modrdn)( be, conn, op, ndn, newrdn,
230                             deloldrdn, newSuperior ) == 0 )
231                         {
232                                 struct replog_moddn moddn;
233                             moddn.newrdn = newrdn;
234                                 moddn.deloldrdn = deloldrdn;
235                                 moddn.newsup = newSuperior;
236                                 replog( be, op, ndn, &moddn );
237                         }
238                 } else {
239                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
240                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
241                 }
242         } else {
243                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
244                         NULL, "Function not implemented", NULL, NULL );
245         }
246
247         free( ndn );
248         free( newrdn ); 
249         free( newSuperior );
250         free( nnewSuperior );
251         return rc;
252 }