]> git.sur5r.net Git - openldap/commitdiff
ITS#8262 add ldap_build_*_req functions
authorHoward Chu <hyc@openldap.org>
Fri, 2 Oct 2015 04:02:15 +0000 (05:02 +0100)
committerQuanah Gibson-Mount <quanah@openldap.org>
Mon, 5 Oct 2015 20:59:33 +0000 (15:59 -0500)
Basic ops except abandon and unbind; since they get no reply
it's not important for the caller to know their msgID.

libraries/libldap/add.c
libraries/libldap/compare.c
libraries/libldap/delete.c
libraries/libldap/ldap-int.h
libraries/libldap/modify.c
libraries/libldap/modrdn.c
libraries/libldap/sasl.c

index 03b334b31a3fa84d3ed02935bf2e408d734803b1..afe4d873b066d831cfc36142dcf226742bba420c 100644 (file)
@@ -85,65 +85,31 @@ ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
 }
 
 
-/*
- * ldap_add_ext - initiate an ldap extended add operation.  Parameters:
- *
- *     ld              LDAP descriptor
- *     dn              DN of the entry to add
- *     mods            List of attributes for the entry.  This is a null-
- *                     terminated array of pointers to LDAPMod structures.
- *                     only the type and values in the structures need be
- *                     filled in.
- *     sctrl   Server Controls
- *     cctrl   Client Controls
- *     msgidp  Message ID pointer
- *
- * Example:
- *     LDAPMod *attrs[] = { 
- *                     { 0, "cn", { "babs jensen", "babs", 0 } },
- *                     { 0, "sn", { "jensen", 0 } },
- *                     { 0, "objectClass", { "person", 0 } },
- *                     0
- *             }
- *     rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
- */
-int
-ldap_add_ext(
+BerElement *
+ldap_build_add_req(
        LDAP *ld,
-       LDAP_CONST char *dn,
+       const char *dn,
        LDAPMod **attrs,
        LDAPControl **sctrls,
        LDAPControl **cctrls,
-       int     *msgidp )
+       ber_int_t       *msgidp )
 {
-       BerElement      *ber;
-       int             i, rc;
-       ber_int_t       id;
-
-       Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
-       assert( ld != NULL );
-       assert( LDAP_VALID( ld ) );
-       assert( dn != NULL );
-       assert( msgidp != NULL );
-
-       /* check client controls */
-       rc = ldap_int_client_controls( ld, cctrls );
-       if( rc != LDAP_SUCCESS ) return rc;
+       BerElement *ber;
+       int i, rc;
 
        /* create a message to send */
        if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
-               ld->ld_errno = LDAP_NO_MEMORY;
-               return ld->ld_errno;
+               return( NULL );
        }
 
-       LDAP_NEXT_MSGID(ld, id);
+       LDAP_NEXT_MSGID(ld, *msgidp);
        rc = ber_printf( ber, "{it{s{", /* '}}}' */
-               id, LDAP_REQ_ADD, dn );
+               *msgidp, LDAP_REQ_ADD, dn );
 
        if ( rc == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return ld->ld_errno;
+               return( NULL );
        }
 
        /* allow attrs to be NULL ("touch"; should fail...) */
@@ -156,14 +122,14 @@ ldap_add_ext(
                                if ( attrs[i]->mod_bvalues == NULL ) {
                                        ld->ld_errno = LDAP_PARAM_ERROR;
                                        ber_free( ber, 1 );
-                                       return ld->ld_errno;
+                                       return( NULL );
                                }
 
                                for ( j = 0; attrs[i]->mod_bvalues[ j ] != NULL; j++ ) {
                                        if ( attrs[i]->mod_bvalues[ j ]->bv_val == NULL ) {
                                                ld->ld_errno = LDAP_PARAM_ERROR;
                                                ber_free( ber, 1 );
-                                               return ld->ld_errno;
+                                               return( NULL );
                                        }
                                }
 
@@ -174,7 +140,7 @@ ldap_add_ext(
                                if ( attrs[i]->mod_values == NULL ) {
                                        ld->ld_errno = LDAP_PARAM_ERROR;
                                        ber_free( ber, 1 );
-                                       return ld->ld_errno;
+                                       return( NULL );
                                }
 
                                rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
@@ -183,7 +149,7 @@ ldap_add_ext(
                        if ( rc == -1 ) {
                                ld->ld_errno = LDAP_ENCODING_ERROR;
                                ber_free( ber, 1 );
-                               return ld->ld_errno;
+                               return( NULL );
                        }
                }
        }
@@ -191,21 +157,73 @@ ldap_add_ext(
        if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return ld->ld_errno;
+               return( NULL );
        }
 
        /* Put Server Controls */
        if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
                ber_free( ber, 1 );
-               return ld->ld_errno;
+               return( NULL );
        }
 
        if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return ld->ld_errno;
+               return( NULL );
        }
 
+       return( ber );
+}
+
+/*
+ * ldap_add_ext - initiate an ldap extended add operation.  Parameters:
+ *
+ *     ld              LDAP descriptor
+ *     dn              DN of the entry to add
+ *     mods            List of attributes for the entry.  This is a null-
+ *                     terminated array of pointers to LDAPMod structures.
+ *                     only the type and values in the structures need be
+ *                     filled in.
+ *     sctrl   Server Controls
+ *     cctrl   Client Controls
+ *     msgidp  Message ID pointer
+ *
+ * Example:
+ *     LDAPMod *attrs[] = {
+ *                     { 0, "cn", { "babs jensen", "babs", 0 } },
+ *                     { 0, "sn", { "jensen", 0 } },
+ *                     { 0, "objectClass", { "person", 0 } },
+ *                     0
+ *             }
+ *     rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
+ */
+int
+ldap_add_ext(
+       LDAP *ld,
+       LDAP_CONST char *dn,
+       LDAPMod **attrs,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       int     *msgidp )
+{
+       BerElement      *ber;
+       int             i, rc;
+       ber_int_t       id;
+
+       Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
+       assert( ld != NULL );
+       assert( LDAP_VALID( ld ) );
+       assert( dn != NULL );
+       assert( msgidp != NULL );
+
+       /* check client controls */
+       rc = ldap_int_client_controls( ld, cctrls );
+       if( rc != LDAP_SUCCESS ) return rc;
+
+       ber = ldap_build_add_req( ld, dn, attrs, sctrls, cctrls, &id );
+       if( !ber )
+               return ld->ld_errno;
+
        /* send the message */
        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber, id );
 
index e7f7ee170e27bea7913f9df61251961cbf3f54e1..dbeb44aee2fa783b6111fe0d80adcd93df6453ce 100644 (file)
  *     }
  */
 
+BerElement *
+ldap_build_compare_req(
+       LDAP *ld,
+       LDAP_CONST char *dn,
+       LDAP_CONST char *attr,
+       struct berval *bvalue,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       int     *msgidp )
+{
+       BerElement      *ber;
+       int rc;
+
+       /* create a message to send */
+       if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
+               return( NULL );
+       }
+
+       LDAP_NEXT_MSGID(ld, *msgidp);
+       rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
+               *msgidp,
+               LDAP_REQ_COMPARE, dn, attr, bvalue );
+       if ( rc == -1 )
+       {
+               ld->ld_errno = LDAP_ENCODING_ERROR;
+               ber_free( ber, 1 );
+               return( NULL );
+       }
+
+       /* Put Server Controls */
+       if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
+               ber_free( ber, 1 );
+               return( NULL );
+       }
+
+       if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
+               ld->ld_errno = LDAP_ENCODING_ERROR;
+               ber_free( ber, 1 );
+               return( NULL );
+       }
+}
+
 /*
  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
  * of the entry to compare to and the attribute and value to compare (in
@@ -74,34 +116,10 @@ ldap_compare_ext(
        rc = ldap_int_client_controls( ld, cctrls );
        if( rc != LDAP_SUCCESS ) return rc;
 
-       /* create a message to send */
-       if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
-               return( LDAP_NO_MEMORY );
-       }
-
-       LDAP_NEXT_MSGID(ld, id);
-       rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
-               id,
-               LDAP_REQ_COMPARE, dn, attr, bvalue );
-       if ( rc == -1 )
-       {
-               ld->ld_errno = LDAP_ENCODING_ERROR;
-               ber_free( ber, 1 );
-               return( ld->ld_errno );
-       }
-
-       /* Put Server Controls */
-       if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
-               ber_free( ber, 1 );
+       ber = ldap_build_compare_req(
+               ld, dn, attr, bvalue, sctrls, cctrls, &id );
+       if( !ber )
                return ld->ld_errno;
-       }
-
-       if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
-               ld->ld_errno = LDAP_ENCODING_ERROR;
-               ber_free( ber, 1 );
-               return( ld->ld_errno );
-       }
-
 
        /* send the message */
        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
index 827d49cfa3ec04c40f700fdd57a2b13ed22e8c3a..be3aecaf747c45735e1fd88c61f4820e047e7ac1 100644 (file)
  *     DelRequet ::= DistinguishedName,
  */
 
+BerElement *
+ldap_build_delete_req(
+       LDAP *ld,
+       LDAP_CONST char *dn,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       int     *msgidp )
+{
+       BerElement      *ber;
+       int rc;
+
+       /* create a message to send */
+       if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
+               return( NULL );
+       }
+
+       LDAP_NEXT_MSGID( ld, *msgidp );
+       rc = ber_printf( ber, "{its", /* '}' */
+               *msgidp, LDAP_REQ_DELETE, dn );
+       if ( rc == -1 )
+       {
+               ld->ld_errno = LDAP_ENCODING_ERROR;
+               ber_free( ber, 1 );
+               return( NULL );
+       }
+
+       /* Put Server Controls */
+       if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
+               ber_free( ber, 1 );
+               return( NULL );
+       }
+
+       if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
+               ld->ld_errno = LDAP_ENCODING_ERROR;
+               ber_free( ber, 1 );
+               return( NULL );
+       }
+
+       return( ber );
+}
 
 /*
  * ldap_delete_ext - initiate an ldap extended delete operation. Parameters:
@@ -67,33 +107,9 @@ ldap_delete_ext(
        rc = ldap_int_client_controls( ld, cctrls );
        if( rc != LDAP_SUCCESS ) return rc;
 
-       /* create a message to send */
-       if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
-               ld->ld_errno = LDAP_NO_MEMORY;
-               return( ld->ld_errno );
-       }
-
-       LDAP_NEXT_MSGID( ld, id );
-       rc = ber_printf( ber, "{its", /* '}' */
-               id, LDAP_REQ_DELETE, dn );
-       if ( rc == -1 )
-       {
-               ld->ld_errno = LDAP_ENCODING_ERROR;
-               ber_free( ber, 1 );
-               return( ld->ld_errno );
-       }
-
-       /* Put Server Controls */
-       if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
-               ber_free( ber, 1 );
+       ber = ldap_build_delete_req( ld, dn, sctrls, cctrls, &id );
+       if( !ber )
                return ld->ld_errno;
-       }
-
-       if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
-               ld->ld_errno = LDAP_ENCODING_ERROR;
-               ber_free( ber, 1 );
-               return( ld->ld_errno );
-       }
 
        /* send the message */
        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_DELETE, dn, ber, id );
index 3d9fd9b3adba72f96b6032e2c79764cde3460373..65e5becbe3b23550cb121e1c283750746bd0c443 100644 (file)
@@ -526,6 +526,42 @@ ldap_int_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx );
 LDAP_F (int)
 ldap_int_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx );
 
+/*
+ * in add.c
+ */
+
+LDAP_F (BerElement *) ldap_build_add_req LDAP_P((
+       LDAP *ld,
+       const char *dn,
+       LDAPMod **attrs,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       ber_int_t *msgidp ));
+
+/*
+ * in compare.c
+ */
+
+LDAP_F (BerElement *) ldap_build_compare_req LDAP_P((
+       LDAP *ld,
+       const char *dn,
+       const char *attr,
+       struct berval *bvalue,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       ber_int_t *msgidp ));
+
+/*
+ * in delete.c
+ */
+
+LDAP_F (BerElement *) ldap_build_delete_req LDAP_P((
+       LDAP *ld,
+       const char *dn,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       ber_int_t *msgidp ));
+
 /*
  * in init.c
  */
@@ -559,6 +595,32 @@ LDAP_F ( void ) ldap_int_initialize_global_options LDAP_P((
  */
 LDAP_F (void) ldap_int_error_init( void );
 
+/*
+ * in modify.c
+ */
+
+LDAP_F (BerElement *) ldap_build_modify_req LDAP_P((
+       LDAP *ld,
+       const char *dn,
+       LDAPMod **mods,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       ber_int_t *msgidp ));
+
+/*
+ * in modrdn.c
+ */
+
+LDAP_F (BerElement *) ldap_build_moddn_req LDAP_P((
+       LDAP *ld,
+       const char *dn,
+       const char *newrdn,
+       const char *newSuperior,
+       int deleteoldrdn,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       ber_int_t *msgidp ));
+
 /*
  * in unit-int.c
  */
@@ -763,6 +825,17 @@ LDAP_F (int) ldap_int_sasl_bind LDAP_P((
        const char **rmech,
        int *msgid ));
 
+/* in sasl.c */
+
+LDAP_F (BerElement *) ldap_build_bind_req LDAP_P((
+       LDAP *ld,
+       const char *dn,
+       const char *mech,
+       struct berval *cred,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       ber_int_t *msgidp ));
+
 /* in schema.c */
 LDAP_F (char *) ldap_int_parse_numericoid LDAP_P((
        const char **sp,
index 9737fc5b1012cfc8a4df9242802a31cf0cca6364..7bbfc794290afccdb9f991a13857ff133fa5e28e 100644 (file)
  * (Source: RFC 4511)
  */
 
-
-/*
- * ldap_modify_ext - initiate an ldap extended modify operation.
- *
- * Parameters:
- *
- *     ld              LDAP descriptor
- *     dn              DN of the object to modify
- *     mods            List of modifications to make.  This is null-terminated
- *                     array of struct ldapmod's, specifying the modifications
- *                     to perform.
- *     sctrls  Server Controls
- *     cctrls  Client Controls
- *     msgidp  Message ID pointer
- *
- * Example:
- *     LDAPMod *mods[] = { 
- *                     { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
- *                     { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },
- *                     { LDAP_MOD_DELETE, "ou", 0 },
- *                     { LDAP_MOD_INCREMENT, "uidNumber, { "1", 0 } }
- *                     0
- *             }
- *     rc=  ldap_modify_ext( ld, dn, mods, sctrls, cctrls, &msgid );
- */
-int
-ldap_modify_ext( LDAP *ld,
+BerElement *
+ldap_build_modify_req(
+       LDAP *ld,
        LDAP_CONST char *dn,
        LDAPMod **mods,
        LDAPControl **sctrls,
        LDAPControl **cctrls,
-       int *msgidp )
+       ber_int_t *msgidp )
 {
        BerElement      *ber;
        int             i, rc;
-       ber_int_t       id;
-
-       Debug( LDAP_DEBUG_TRACE, "ldap_modify_ext\n", 0, 0, 0 );
-
-       /* check client controls */
-       rc = ldap_int_client_controls( ld, cctrls );
-       if( rc != LDAP_SUCCESS ) return rc;
 
        /* create a message to send */
        if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
-               return( LDAP_NO_MEMORY );
+               return( NULL );
        }
 
-       LDAP_NEXT_MSGID( ld, id );
-       rc = ber_printf( ber, "{it{s{" /*}}}*/, id, LDAP_REQ_MODIFY, dn );
+       LDAP_NEXT_MSGID( ld, *msgidp );
+       rc = ber_printf( ber, "{it{s{" /*}}}*/, *msgidp, LDAP_REQ_MODIFY, dn );
        if ( rc == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return( ld->ld_errno );
+               return( NULL );
        }
 
        /* allow mods to be NULL ("touch") */
@@ -124,7 +93,7 @@ ldap_modify_ext( LDAP *ld,
                        if ( rc == -1 ) {
                                ld->ld_errno = LDAP_ENCODING_ERROR;
                                ber_free( ber, 1 );
-                               return( ld->ld_errno );
+                               return( NULL );
                        }
                }
        }
@@ -132,21 +101,70 @@ ldap_modify_ext( LDAP *ld,
        if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return( ld->ld_errno );
+               return( NULL );
        }
 
        /* Put Server Controls */
        if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
                ber_free( ber, 1 );
-               return ld->ld_errno;
+               return( NULL );
        }
 
        if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return( ld->ld_errno );
+               return( NULL );
        }
 
+       return( ber );
+}
+
+/*
+ * ldap_modify_ext - initiate an ldap extended modify operation.
+ *
+ * Parameters:
+ *
+ *     ld              LDAP descriptor
+ *     dn              DN of the object to modify
+ *     mods            List of modifications to make.  This is null-terminated
+ *                     array of struct ldapmod's, specifying the modifications
+ *                     to perform.
+ *     sctrls  Server Controls
+ *     cctrls  Client Controls
+ *     msgidp  Message ID pointer
+ *
+ * Example:
+ *     LDAPMod *mods[] = {
+ *                     { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
+ *                     { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },
+ *                     { LDAP_MOD_DELETE, "ou", 0 },
+ *                     { LDAP_MOD_INCREMENT, "uidNumber, { "1", 0 } }
+ *                     0
+ *             }
+ *     rc=  ldap_modify_ext( ld, dn, mods, sctrls, cctrls, &msgid );
+ */
+int
+ldap_modify_ext( LDAP *ld,
+       LDAP_CONST char *dn,
+       LDAPMod **mods,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       int *msgidp )
+{
+       BerElement      *ber;
+       int             i, rc;
+       ber_int_t       id;
+
+       Debug( LDAP_DEBUG_TRACE, "ldap_modify_ext\n", 0, 0, 0 );
+
+       /* check client controls */
+       rc = ldap_int_client_controls( ld, cctrls );
+       if( rc != LDAP_SUCCESS ) return rc;
+
+       ber = ldap_build_modify_req( ld, dn, mods, sctrls, cctrls, &id );
+       if( !ber )
+               return ld->ld_errno;
+
        /* send the message */
        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber, id );
        return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
@@ -164,7 +182,7 @@ ldap_modify_ext( LDAP *ld,
  *                     to perform.
  *
  * Example:
- *     LDAPMod *mods[] = { 
+ *     LDAPMod *mods[] = {
  *                     { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
  *                     { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },
  *                     { LDAP_MOD_DELETE, "ou", 0 },
index 6980593630e9d812e6de764f203948d1bba94b60..5a50084bc46ab64eaa3d6c9a7f956f2423912bab 100644 (file)
  *     }
  */
 
-
-/*
- * ldap_rename - initiate an ldap extended modifyDN operation.
- *
- * Parameters:
- *     ld                              LDAP descriptor
- *     dn                              DN of the object to modify
- *     newrdn                  RDN to give the object
- *     deleteoldrdn    nonzero means to delete old rdn values from the entry
- *     newSuperior             DN of the new parent if applicable
- *
- * Returns the LDAP error code.
- */
-
-int
-ldap_rename(
+BerElement *
+ldap_build_moddn_req(
        LDAP *ld,
        LDAP_CONST char *dn,
        LDAP_CONST char *newrdn,
@@ -71,61 +57,96 @@ ldap_rename(
        int deleteoldrdn,
        LDAPControl **sctrls,
        LDAPControl **cctrls,
-       int *msgidp )
+       ber_int_t *msgidp )
 {
        BerElement      *ber;
        int rc;
-       ber_int_t id;
-
-       Debug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );
-
-       /* check client controls */
-       rc = ldap_int_client_controls( ld, cctrls );
-       if( rc != LDAP_SUCCESS ) return rc;
 
        /* create a message to send */
        if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
-               return( LDAP_NO_MEMORY );
+               return( NULL );
        }
 
-       LDAP_NEXT_MSGID( ld, id );
+       LDAP_NEXT_MSGID( ld, *msgidp );
        if( newSuperior != NULL ) {
                /* must be version 3 (or greater) */
                if ( ld->ld_version < LDAP_VERSION3 ) {
                        ld->ld_errno = LDAP_NOT_SUPPORTED;
                        ber_free( ber, 1 );
-                       return( ld->ld_errno );
+                       return( NULL );
                }
                rc = ber_printf( ber, "{it{ssbtsN}", /* '}' */ 
-                       id, LDAP_REQ_MODDN,
+                       *msgidp, LDAP_REQ_MODDN,
                        dn, newrdn, (ber_int_t) deleteoldrdn,
                        LDAP_TAG_NEWSUPERIOR, newSuperior );
 
        } else {
                rc = ber_printf( ber, "{it{ssbN}", /* '}' */ 
-                       id, LDAP_REQ_MODDN,
+                       *msgidp, LDAP_REQ_MODDN,
                        dn, newrdn, (ber_int_t) deleteoldrdn );
        }
 
        if ( rc < 0 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return( ld->ld_errno );
+               return( NULL );
        }
 
        /* Put Server Controls */
        if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
                ber_free( ber, 1 );
-               return ld->ld_errno;
+               return( NULL );
        }
 
        rc = ber_printf( ber, /*{*/ "N}" );
        if ( rc < 0 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return( ld->ld_errno );
+               return( NULL );
        }
 
+       return( ber );
+}
+
+/*
+ * ldap_rename - initiate an ldap extended modifyDN operation.
+ *
+ * Parameters:
+ *     ld                              LDAP descriptor
+ *     dn                              DN of the object to modify
+ *     newrdn                  RDN to give the object
+ *     deleteoldrdn    nonzero means to delete old rdn values from the entry
+ *     newSuperior             DN of the new parent if applicable
+ *
+ * Returns the LDAP error code.
+ */
+
+int
+ldap_rename(
+       LDAP *ld,
+       LDAP_CONST char *dn,
+       LDAP_CONST char *newrdn,
+       LDAP_CONST char *newSuperior,
+       int deleteoldrdn,
+       LDAPControl **sctrls,
+       LDAPControl **cctrls,
+       int *msgidp )
+{
+       BerElement      *ber;
+       int rc;
+       ber_int_t id;
+
+       Debug( LDAP_DEBUG_TRACE, "ldap_rename\n", 0, 0, 0 );
+
+       /* check client controls */
+       rc = ldap_int_client_controls( ld, cctrls );
+       if( rc != LDAP_SUCCESS ) return rc;
+
+       ber = ldap_build_moddn_req( ld, dn, newrdn, newSuperior,
+               deleteoldrdn, sctrls, cctrls, &id );
+       if( !ber )
+               return ld->ld_errno;
+
        /* send the message */
        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODRDN, dn, ber, id );
        
index a49cad64b8f43bbb95b634dd075cd6d1312672b0..3f8c497d123c40ffcacde1b549c920861f0c2f86 100644 (file)
 
 #include "ldap-int.h"
 
-/*
- * ldap_sasl_bind - bind to the ldap server (and X.500).
- * The dn (usually NULL), mechanism, and credentials are provided.
- * The message id of the request initiated is provided upon successful
- * (LDAP_SUCCESS) return.
- *
- * Example:
- *     ldap_sasl_bind( ld, NULL, "mechanism",
- *             cred, NULL, NULL, &msgid )
- */
-
-int
-ldap_sasl_bind(
+BerElement *
+ldap_build_bind_req(
        LDAP                    *ld,
        LDAP_CONST char *dn,
        LDAP_CONST char *mechanism,
        struct berval   *cred,
        LDAPControl             **sctrls,
        LDAPControl             **cctrls,
-       int                             *msgidp )
+       ber_int_t               *msgidp )
 {
        BerElement      *ber;
        int rc;
-       ber_int_t id;
-
-       Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
-
-       assert( ld != NULL );
-       assert( LDAP_VALID( ld ) );
-       assert( msgidp != NULL );
-
-       /* check client controls */
-       rc = ldap_int_client_controls( ld, cctrls );
-       if( rc != LDAP_SUCCESS ) return rc;
 
        if( mechanism == LDAP_SASL_SIMPLE ) {
                if( dn == NULL && cred != NULL && cred->bv_len ) {
@@ -87,7 +65,7 @@ ldap_sasl_bind(
 
        } else if( ld->ld_version < LDAP_VERSION3 ) {
                ld->ld_errno = LDAP_NOT_SUPPORTED;
-               return ld->ld_errno;
+               return( NULL );
        }
 
        if ( dn == NULL ) {
@@ -96,31 +74,28 @@ ldap_sasl_bind(
 
        /* create a message to send */
        if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
-               ld->ld_errno = LDAP_NO_MEMORY;
-               return ld->ld_errno;
+               return( NULL );
        }
 
-       assert( LBER_VALID( ber ) );
-
-       LDAP_NEXT_MSGID( ld, id );
+       LDAP_NEXT_MSGID( ld, *msgidp );
        if( mechanism == LDAP_SASL_SIMPLE ) {
                /* simple bind */
                rc = ber_printf( ber, "{it{istON}" /*}*/,
-                       id, LDAP_REQ_BIND,
+                       *msgidp, LDAP_REQ_BIND,
                        ld->ld_version, dn, LDAP_AUTH_SIMPLE,
                        cred );
                
        } else if ( cred == NULL || cred->bv_val == NULL ) {
                /* SASL bind w/o credentials */
                rc = ber_printf( ber, "{it{ist{sN}N}" /*}*/,
-                       id, LDAP_REQ_BIND,
+                       *msgidp, LDAP_REQ_BIND,
                        ld->ld_version, dn, LDAP_AUTH_SASL,
                        mechanism );
 
        } else {
                /* SASL bind w/ credentials */
                rc = ber_printf( ber, "{it{ist{sON}N}" /*}*/,
-                       id, LDAP_REQ_BIND,
+                       *msgidp, LDAP_REQ_BIND,
                        ld->ld_version, dn, LDAP_AUTH_SASL,
                        mechanism, cred );
        }
@@ -128,21 +103,62 @@ ldap_sasl_bind(
        if( rc == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return( -1 );
+               return( NULL );
        }
 
        /* Put Server Controls */
        if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
                ber_free( ber, 1 );
-               return ld->ld_errno;
+               return( NULL );
        }
 
        if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                ber_free( ber, 1 );
-               return ld->ld_errno;
+               return( NULL );
        }
 
+       return( ber );
+}
+
+/*
+ * ldap_sasl_bind - bind to the ldap server (and X.500).
+ * The dn (usually NULL), mechanism, and credentials are provided.
+ * The message id of the request initiated is provided upon successful
+ * (LDAP_SUCCESS) return.
+ *
+ * Example:
+ *     ldap_sasl_bind( ld, NULL, "mechanism",
+ *             cred, NULL, NULL, &msgid )
+ */
+
+int
+ldap_sasl_bind(
+       LDAP                    *ld,
+       LDAP_CONST char *dn,
+       LDAP_CONST char *mechanism,
+       struct berval   *cred,
+       LDAPControl             **sctrls,
+       LDAPControl             **cctrls,
+       int                             *msgidp )
+{
+       BerElement      *ber;
+       int rc;
+       ber_int_t id;
+
+       Debug( LDAP_DEBUG_TRACE, "ldap_sasl_bind\n", 0, 0, 0 );
+
+       assert( ld != NULL );
+       assert( LDAP_VALID( ld ) );
+       assert( msgidp != NULL );
+
+       /* check client controls */
+       rc = ldap_int_client_controls( ld, cctrls );
+       if( rc != LDAP_SUCCESS ) return rc;
+
+       ber = ldap_build_bind_req( ld, dn, mechanism, cred, sctrls, cctrls, &id );
+       if( !ber )
+               return ld->ld_errno;
 
        /* send the message */
        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber, id );