]> git.sur5r.net Git - openldap/blob - servers/slapd/modrdn.c
Use calloc properly... could result in too few bytes being allocated.
[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         int     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         unsigned long   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         Debug( LDAP_DEBUG_ARGS,
71             "do_modrdn: dn (%s) newrdn (%s) deloldrdn (%d)\n", ndn, newrdn,
72             deloldrdn );
73
74
75         /* Check for newSuperior parameter, if present scan it */
76
77         if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR ) {
78
79                 if ( conn->c_protocol == LDAP_VERSION2 ) {
80
81                         /* Conection record indicates v2 but field 
82                          * newSuperior is present: report error.
83                          */
84                         Debug( LDAP_DEBUG_ANY,
85                                "modrdn(v2) has field newSuperior!\n",
86                                0, 0, 0 );
87                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
88                                           NULL, "" );
89                         return;
90
91                 } else if ( conn->c_protocol ==  0 ) {
92
93                         /* The other side is talking v3 but did not Bind as v3
94                          * so we accept this and set the connection record
95                          * accordingly.
96                          */
97                     
98                         conn->c_protocol = LDAP_VERSION3;
99
100                 }/* else if ( conn->c_protocol ==  0 ) */
101
102
103                 if ( ber_scanf( op->o_ber, "a}", &newSuperior ) 
104                      == LBER_ERROR ) {
105
106                     Debug( LDAP_DEBUG_ANY, "ber_scanf(\"a\"}) failed\n",
107                            0, 0, 0 );
108                     send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
109                                       "" );
110                     return;
111
112                 }/* if ( ber_scanf( ber, "a}", &newSuperior ) == ... ) */
113
114
115                 Debug( LDAP_DEBUG_ARGS, "do_modrdn: newSuperior=(%s)\n",
116                        newSuperior, 0, 0 );
117
118                 /* GET BACKEND FOR NEW SUPERIOR */
119
120                 nnewSuperior = strdup( newSuperior );
121                 dn_normalize_case( nnewSuperior );
122
123                 if ( (newSuperior_be = select_backend( nnewSuperior )) 
124                      == NULL ) {
125                     
126                         /* We do not have a backend for newSuperior so we send
127                          * a referral.
128                          * XXX: We may need to do something else here, not sure
129                          * what though.
130                          */
131                 
132
133                         Debug( LDAP_DEBUG_ARGS,
134                                "do_modrdn: cant find backend for=(%s)\n",
135                                newSuperior, 0, 0 );
136                         
137                         free( ndn );
138                         free( newrdn );
139                         free( newSuperior );
140                         free( nnewSuperior );
141                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
142                                           default_referral );
143                         return;
144                         
145                 }
146
147         }/* if ( ber_peek_tag( op->o_ber, &length ) == LDAP_TAG_NEWSUPERIOR )*/
148
149         dn_normalize_case( ndn );
150
151         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MODRDN dn=\"%s\"\n",
152             conn->c_connid, op->o_opid, ndn, 0, 0 );
153
154         /*
155          * We could be serving multiple database backends.  Select the
156          * appropriate one, or send a referral to our "referral server"
157          * if we don't hold it.
158          */
159
160         if ( (be = select_backend( ndn )) == NULL ) {
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;
168         }
169
170         /* Make sure that the entry being changed and the newSuperior are in 
171          * the same backend, otherwise we return an error.
172          */
173
174         if ( (newSuperior_be != NULL) && ( be != newSuperior_be) ) {
175
176                 Debug( LDAP_DEBUG_ANY, "dn=(%s), newSuperior=(%s)\n", ndn,
177                        newSuperior, 0 );
178                 
179                 free( ndn );
180                 free( newrdn );
181                 free( newSuperior );
182                 free( nnewSuperior );
183                 
184                 send_ldap_result( conn, op, LDAP_AFFECTS_MULTIPLE_DSAS,
185                                   NULL, "" );
186             
187                 return;
188
189         }/* if ( (newSuperior_be != NULL) && ( be != newSuperior_be) ) */
190
191
192         /* alias suffix if approp */
193         ndn = suffixAlias( ndn, op, be );
194
195         /*
196          * do the add if 1 && (2 || 3)
197          * 1) there is an add function implemented in this backend;
198          * 2) this backend is master for what it holds;
199          * 3) it's a replica and the dn supplied is the update_ndn.
200          */
201         if ( be->be_modrdn ) {
202                 /* do the update here */
203                 if ( be->be_update_ndn == NULL ||
204                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
205                 {
206                         if ( (*be->be_modrdn)( be, conn, op, ndn, newrdn,
207                             deloldrdn, newSuperior ) == 0 ) {
208                                 /* XXX: MAY NEEED TO ADD newSuperior HERE */
209                                 replog( be, LDAP_REQ_MODRDN, ndn, newrdn,
210                                     deloldrdn );
211                         }
212                 } else {
213                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
214                             default_referral );
215                 }
216         } else {
217                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
218                     "Function not implemented" );
219         }
220
221         free( ndn );
222         free( newrdn ); 
223         free( newSuperior );
224         free( nnewSuperior );
225 }