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