summaryrefslogtreecommitdiffstats
path: root/ldap/c-sdk/libldap/rename.c
blob: ac3ec7c737421ea5a2eb18472f44d2be830cd81b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The Original Code is Mozilla Communicator client code, released
 * March 31, 1998.
 * 
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998-1999
 * the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */
/*
 *  Copyright (c) 1990 Regents of the University of Michigan.
 *  All rights reserved.
 */
/*
 *  rename.c
 */

#if 0
#ifndef lint 
static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
#endif
#endif

#include "ldap-int.h"

/*
 * ldap_rename - initiate an ldap modifyDN operation. Parameters:
 *
 *	ld		LDAP descriptor
 *	dn		DN of the object to modify
 *	newrdn		RDN that will form leftmost component of entry's new name
 *      newparent       if present, this is the Distinguished Name of the entry
 *                      which becomes the immediate parent of the existing entry
 *	deleteoldrdn	nonzero means to delete old rdn values from the entry
 *                      while zero means to retain them as attributes of the entry
 *      serverctrls     list of LDAP server controls
 *      clientctrls     list of client controls
 *      msgidp          this result parameter will be set to the message id of the
 *                      request if the ldap_rename() call succeeds
 *
 * Example:
 *      int rc;
 *	rc = ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid );
 */
int
LDAP_CALL
ldap_rename( 
	   LDAP *ld, 
	   const char *dn, 
	   const char *newrdn, 
	   const char *newparent,
	   int deleteoldrdn, 
	   LDAPControl	**serverctrls,
	   LDAPControl	**clientctrls,  /* not used for anything yet */
	   int *msgidp
)
{
	BerElement	*ber;
	int		rc, err;

	/*
	 * A modify dn request looks like this:
	 *	ModifyDNRequest ::= SEQUENCE {
	 *		entry		LDAPDN,
	 *		newrdn		RelativeLDAPDN,
	 *              newparent       [0] LDAPDN OPTIONAL,
	 *		deleteoldrdn	BOOLEAN
	 *	}
	 */

	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );

	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
		return( LDAP_PARAM_ERROR );
	}
	if ( NULL == newrdn) {
		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
		return( LDAP_PARAM_ERROR );
	}

	/* only ldapv3 or higher can do a proper rename
	 * (i.e. with non-NULL newparent and/or controls)
	 */

	if (( NSLDAPI_LDAP_VERSION( ld ) < LDAP_VERSION3 )
	    && ((newparent != NULL) || (serverctrls != NULL)
	    || (clientctrls != NULL))) {
		LDAP_SET_LDERRNO( ld, LDAP_NOT_SUPPORTED, NULL, NULL );
		return( LDAP_NOT_SUPPORTED );
	}

	if ( msgidp == NULL ) {
		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
                return( LDAP_PARAM_ERROR );
        }

	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
	*msgidp = ++ld->ld_msgid;
        LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );

	/* see if modRDN or modDN is handled by the cache */
 	if ( ld->ld_cache_on ) {
		if ( newparent == NULL && ld->ld_cache_modrdn != NULL ) {
			LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
			if ( (rc = (ld->ld_cache_modrdn)( ld, *msgidp,
			    LDAP_REQ_MODRDN, dn, newrdn, deleteoldrdn ))
			    != 0 ) {   
				*msgidp = rc;
				LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
				return( LDAP_SUCCESS );
			}
			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
#if 0
		} else if ( ld->ld_cache_rename != NULL ) {
			LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
			if ( (rc = (ld->ld_cache_rename)( ld, *msgidp,
			    LDAP_REQ_MODDN, dn, newrdn, newparent,
			    deleteoldrdn )) != 0 ) {   
				*msgidp = rc;
				return( LDAP_SUCCESS );
			}
			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
#endif
		}
	}

	/* create a message to send */
	if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
	    != LDAP_SUCCESS ) {
		return( err );
	}

	/* fill it in */
	if ( ber_printf( ber, "{it{ssb", *msgidp, LDAP_REQ_MODDN, dn,
	    newrdn, deleteoldrdn ) == -1 ) {
		LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
		ber_free( ber, 1 );
		return( LDAP_ENCODING_ERROR );
	}

	if ( newparent == NULL ) {
		if ( ber_printf( ber, "}" ) == -1 ) {
			LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
			ber_free( ber, 1 );
			return( LDAP_ENCODING_ERROR );
		}
	} else {
		if ( ber_printf( ber, "ts}", LDAP_TAG_NEWSUPERIOR, newparent )
		    == -1 ) {
			LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
			ber_free( ber, 1 );
			return( LDAP_ENCODING_ERROR );
		}
	}

	if (( rc = nsldapi_put_controls( ld, serverctrls, 1, ber ))
	    != LDAP_SUCCESS ) {
		ber_free( ber, 1 );
		return( rc );
	}

	/* send the message */
	rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_MODDN,
		(char *) dn, ber );
	*msgidp = rc;
	return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
}

int
LDAP_CALL
ldap_modrdn2( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
{
	int             msgid;

	if ( ldap_rename( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL, &msgid ) == LDAP_SUCCESS ) {
		return( msgid );
	} else {
		return( -1 );	/* error is in ld handle */
	}
}

int
LDAP_CALL
ldap_modrdn( LDAP *ld, const char *dn, const char *newrdn )
{
	return( ldap_modrdn2( ld, dn, newrdn, 1 ) );
}

int
LDAP_CALL
ldap_rename_s( 
	   LDAP *ld, 
	   const char *dn, 
	   const char *newrdn, 
	   const char *newparent,
	   int deleteoldrdn, 
	   LDAPControl	**serverctrls,
	   LDAPControl	**clientctrls  /* not used for anything yet */
)
{
	int		msgid;
	LDAPMessage	*res;

	if ( ldap_rename( ld, dn, newrdn, newparent, deleteoldrdn, serverctrls, clientctrls, &msgid ) != LDAP_SUCCESS ) {
		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
	}

 	if ( msgid == -1 ) 
		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );

	if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );

	return( ldap_result2error( ld, res, 1 ) );
}

int
LDAP_CALL
ldap_modrdn2_s( LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn )
{
        int             msgid;
        LDAPMessage     *res;
 
        if ( (msgid = ldap_modrdn2( ld, dn, newrdn, deleteoldrdn )) == -1 )
                return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
 
        if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
                return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
 
        return( ldap_result2error( ld, res, 1 ) );
}

int
LDAP_CALL
ldap_modrdn_s( LDAP *ld, const char *dn, const char *newrdn )
{
	return( ldap_modrdn2_s( ld, dn, newrdn, 1 ) );
}