From: Howard Chu Date: Fri, 2 Oct 2015 04:02:15 +0000 (+0100) Subject: ITS#8262 add ldap_build_*_req functions X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=34ccd14f3e8c6a02ceae11c7b0d475e17d011984;p=openldap ITS#8262 add ldap_build_*_req functions Basic ops except abandon and unbind; since they get no reply it's not important for the caller to know their msgID. --- diff --git a/libraries/libldap/add.c b/libraries/libldap/add.c index 03b334b31a..afe4d873b0 100644 --- a/libraries/libldap/add.c +++ b/libraries/libldap/add.c @@ -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 ); diff --git a/libraries/libldap/compare.c b/libraries/libldap/compare.c index e7f7ee170e..dbeb44aee2 100644 --- a/libraries/libldap/compare.c +++ b/libraries/libldap/compare.c @@ -37,6 +37,48 @@ * } */ +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 ); diff --git a/libraries/libldap/delete.c b/libraries/libldap/delete.c index 827d49cfa3..be3aecaf74 100644 --- a/libraries/libldap/delete.c +++ b/libraries/libldap/delete.c @@ -31,6 +31,46 @@ * 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 ); diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h index 5482572539..5f74c0906a 100644 --- a/libraries/libldap/ldap-int.h +++ b/libraries/libldap/ldap-int.h @@ -530,6 +530,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 */ @@ -563,6 +599,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 */ @@ -759,6 +821,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, diff --git a/libraries/libldap/modify.c b/libraries/libldap/modify.c index 9737fc5b10..7bbfc79429 100644 --- a/libraries/libldap/modify.c +++ b/libraries/libldap/modify.c @@ -51,60 +51,29 @@ * (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 }, diff --git a/libraries/libldap/modrdn.c b/libraries/libldap/modrdn.c index 6980593630..5a50084bc4 100644 --- a/libraries/libldap/modrdn.c +++ b/libraries/libldap/modrdn.c @@ -48,22 +48,8 @@ * } */ - -/* - * 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 ); diff --git a/libraries/libldap/sasl.c b/libraries/libldap/sasl.c index a49cad64b8..3f8c497d12 100644 --- a/libraries/libldap/sasl.c +++ b/libraries/libldap/sasl.c @@ -44,40 +44,18 @@ #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 );