Home | History | Annotate | Line # | Download | only in libldap
modrdn.c revision 1.1.1.2
      1 /*	$NetBSD: modrdn.c,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $	*/
      2 
      3 /* OpenLDAP: pkg/ldap/libraries/libldap/modrdn.c,v 1.30.2.4 2009/01/22 00:00:54 kurt Exp */
      4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5  *
      6  * Copyright 1998-2009 The OpenLDAP Foundation.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted only as authorized by the OpenLDAP
     11  * Public License.
     12  *
     13  * A copy of this license is available in the file LICENSE in the
     14  * top-level directory of the distribution or, alternatively, at
     15  * <http://www.OpenLDAP.org/license.html>.
     16  */
     17 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
     18  * All rights reserved.
     19  */
     20 /* Copyright 1999, Juan C. Gomez, All rights reserved.
     21  * This software is not subject to any license of Silicon Graphics
     22  * Inc. or Purdue University.
     23  *
     24  * Redistribution and use in source and binary forms are permitted
     25  * without restriction or fee of any kind as long as this notice
     26  * is preserved.
     27  */
     28 
     29 /* ACKNOWLEDGEMENTS:
     30  * 	Juan C. Gomez
     31  */
     32 
     33 #include "portable.h"
     34 
     35 #include <stdio.h>
     36 
     37 #include <ac/socket.h>
     38 #include <ac/string.h>
     39 #include <ac/time.h>
     40 
     41 #include "ldap-int.h"
     42 
     43 /*
     44  * A modify rdn request looks like this:
     45  *	ModifyRDNRequest ::= SEQUENCE {
     46  *		entry		DistinguishedName,
     47  *		newrdn		RelativeDistinguishedName,
     48  *		deleteoldrdn	BOOLEAN
     49  *		newSuperior	[0] DistinguishedName	[v3 only]
     50  *	}
     51  */
     52 
     53 
     54 /*
     55  * ldap_rename - initiate an ldap extended modifyDN operation.
     56  *
     57  * Parameters:
     58  *	ld				LDAP descriptor
     59  *	dn				DN of the object to modify
     60  *	newrdn			RDN to give the object
     61  *	deleteoldrdn	nonzero means to delete old rdn values from the entry
     62  *	newSuperior		DN of the new parent if applicable
     63  *
     64  * Returns the LDAP error code.
     65  */
     66 
     67 int
     68 ldap_rename(
     69 	LDAP *ld,
     70 	LDAP_CONST char *dn,
     71 	LDAP_CONST char *newrdn,
     72 	LDAP_CONST char *newSuperior,
     73 	int deleteoldrdn,
     74 	LDAPControl **sctrls,
     75 	LDAPControl **cctrls,
     76 	int *msgidp )
     77 {
     78 	BerElement	*ber;
     79 	int rc;
     80 	ber_int_t id;
     81 
     82 	Debug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );
     83 
     84 	/* check client controls */
     85 	rc = ldap_int_client_controls( ld, cctrls );
     86 	if( rc != LDAP_SUCCESS ) return rc;
     87 
     88 	/* create a message to send */
     89 	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
     90 		return( LDAP_NO_MEMORY );
     91 	}
     92 
     93 	LDAP_NEXT_MSGID( ld, id );
     94 	if( newSuperior != NULL ) {
     95 		/* must be version 3 (or greater) */
     96 		if ( ld->ld_version < LDAP_VERSION3 ) {
     97 			ld->ld_errno = LDAP_NOT_SUPPORTED;
     98 			ber_free( ber, 1 );
     99 			return( ld->ld_errno );
    100 		}
    101 		rc = ber_printf( ber, "{it{ssbtsN}", /* '}' */
    102 			id, LDAP_REQ_MODDN,
    103 			dn, newrdn, (ber_int_t) deleteoldrdn,
    104 			LDAP_TAG_NEWSUPERIOR, newSuperior );
    105 
    106 	} else {
    107 		rc = ber_printf( ber, "{it{ssbN}", /* '}' */
    108 			id, LDAP_REQ_MODDN,
    109 			dn, newrdn, (ber_int_t) deleteoldrdn );
    110 	}
    111 
    112 	if ( rc < 0 ) {
    113 		ld->ld_errno = LDAP_ENCODING_ERROR;
    114 		ber_free( ber, 1 );
    115 		return( ld->ld_errno );
    116 	}
    117 
    118 	/* Put Server Controls */
    119 	if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
    120 		ber_free( ber, 1 );
    121 		return ld->ld_errno;
    122 	}
    123 
    124 	rc = ber_printf( ber, /*{*/ "N}" );
    125 	if ( rc < 0 ) {
    126 		ld->ld_errno = LDAP_ENCODING_ERROR;
    127 		ber_free( ber, 1 );
    128 		return( ld->ld_errno );
    129 	}
    130 
    131 	/* send the message */
    132 	*msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODRDN, dn, ber, id );
    133 
    134 	if( *msgidp < 0 ) {
    135 		return( ld->ld_errno );
    136 	}
    137 
    138 	return LDAP_SUCCESS;
    139 }
    140 
    141 
    142 /*
    143  * ldap_rename2 - initiate an ldap (and X.500) modifyDN operation. Parameters:
    144  *	(LDAP V3 MODIFYDN REQUEST)
    145  *	ld		LDAP descriptor
    146  *	dn		DN of the object to modify
    147  *	newrdn		RDN to give the object
    148  *	deleteoldrdn	nonzero means to delete old rdn values from the entry
    149  *	newSuperior	DN of the new parent if applicable
    150  *
    151  * ldap_rename2 uses a U-Mich Style API.  It returns the msgid.
    152  */
    153 
    154 int
    155 ldap_rename2(
    156 	LDAP *ld,
    157 	LDAP_CONST char *dn,
    158 	LDAP_CONST char *newrdn,
    159 	LDAP_CONST char *newSuperior,
    160 	int deleteoldrdn )
    161 {
    162 	int msgid;
    163 	int rc;
    164 
    165 	Debug( LDAP_DEBUG_TRACE, "ldap_rename2\n", 0, 0, 0 );
    166 
    167 	rc = ldap_rename( ld, dn, newrdn, newSuperior,
    168 		deleteoldrdn, NULL, NULL, &msgid );
    169 
    170 	return rc == LDAP_SUCCESS ? msgid : -1;
    171 }
    172 
    173 
    174 /*
    175  * ldap_modrdn2 - initiate an ldap modifyRDN operation. Parameters:
    176  *
    177  *	ld		LDAP descriptor
    178  *	dn		DN of the object to modify
    179  *	newrdn		RDN to give the object
    180  *	deleteoldrdn	nonzero means to delete old rdn values from the entry
    181  *
    182  * Example:
    183  *	msgid = ldap_modrdn( ld, dn, newrdn );
    184  */
    185 int
    186 ldap_modrdn2( LDAP *ld,
    187 	LDAP_CONST char *dn,
    188 	LDAP_CONST char *newrdn,
    189 	int deleteoldrdn )
    190 {
    191 	return ldap_rename2( ld, dn, newrdn, NULL, deleteoldrdn );
    192 }
    193 
    194 int
    195 ldap_modrdn( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn )
    196 {
    197 	return( ldap_rename2( ld, dn, newrdn, NULL, 1 ) );
    198 }
    199 
    200 
    201 int
    202 ldap_rename_s(
    203 	LDAP *ld,
    204 	LDAP_CONST char *dn,
    205 	LDAP_CONST char *newrdn,
    206 	LDAP_CONST char *newSuperior,
    207 	int deleteoldrdn,
    208 	LDAPControl **sctrls,
    209 	LDAPControl **cctrls )
    210 {
    211 	int rc;
    212 	int msgid;
    213 	LDAPMessage *res;
    214 
    215 	rc = ldap_rename( ld, dn, newrdn, newSuperior,
    216 		deleteoldrdn, sctrls, cctrls, &msgid );
    217 
    218 	if( rc != LDAP_SUCCESS ) {
    219 		return rc;
    220 	}
    221 
    222 	rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &res );
    223 
    224 	if( rc == -1 || !res ) {
    225 		return ld->ld_errno;
    226 	}
    227 
    228 	return ldap_result2error( ld, res, 1 );
    229 }
    230 
    231 int
    232 ldap_rename2_s(
    233 	LDAP *ld,
    234 	LDAP_CONST char *dn,
    235 	LDAP_CONST char *newrdn,
    236 	LDAP_CONST char *newSuperior,
    237 	int deleteoldrdn )
    238 {
    239 	return ldap_rename_s( ld, dn, newrdn, newSuperior,
    240 		deleteoldrdn, NULL, NULL );
    241 }
    242 
    243 int
    244 ldap_modrdn2_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn, int deleteoldrdn )
    245 {
    246 	return ldap_rename_s( ld, dn, newrdn, NULL, deleteoldrdn, NULL, NULL );
    247 }
    248 
    249 int
    250 ldap_modrdn_s( LDAP *ld, LDAP_CONST char *dn, LDAP_CONST char *newrdn )
    251 {
    252 	return ldap_rename_s( ld, dn, newrdn, NULL, 1, NULL, NULL );
    253 }
    254 
    255