ULONG CDECL ldap_parse_sort_controlW( WLDAP32_LDAP *ld, PLDAPControlW *control, ULONG *result, PWCHAR *attr ) { ULONG ret = LDAP_NOT_SUPPORTED; #ifdef HAVE_LDAP char *attrU = NULL; LDAPControl **controlU = NULL; TRACE( "(%p, %p, %p, %p)\n", ld, control, result, attr ); if (!ld) return ~0UL; if (control) { controlU = controlarrayWtoU( control ); if (!controlU) return WLDAP32_LDAP_NO_MEMORY; } ret = ldap_parse_sort_control( ld, controlU, result, &attrU ); *attr = strUtoW( attrU ); controlarrayfreeU( controlU ); #endif return ret; }
/*********************************************************************** * ldap_parse_sort_controlW (WLDAP32.@) * * Parse a sort control. * * PARAMS * ld [I] Pointer to an LDAP context. * control [I] Control obtained from a result message. * result [O] Result code. * attr [O] Failing attribute. * * RETURNS * Success: LDAP_SUCCESS * Failure: An LDAP error code. * * NOTES * If the function fails, free the failing attribute with ldap_memfree. */ ULONG CDECL ldap_parse_sort_controlW( WLDAP32_LDAP *ld, PLDAPControlW *control, ULONG *result, PWCHAR *attr ) { ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED; #ifdef HAVE_LDAP char *attrU = NULL; LDAPControl **controlU = NULL; #ifdef HAVE_LDAP_PARSE_SORT_CONTROL unsigned long res; #elif defined(HAVE_LDAP_PARSE_SORTRESPONSE_CONTROL) ber_int_t res; LDAPControl *sortcontrol = NULL; unsigned int i; #endif TRACE( "(%p, %p, %p, %p)\n", ld, control, result, attr ); if (!ld) return WLDAP32_LDAP_PARAM_ERROR; if (!control) return WLDAP32_LDAP_CONTROL_NOT_FOUND; controlU = controlarrayWtoU( control ); if (!controlU) return WLDAP32_LDAP_NO_MEMORY; #ifdef HAVE_LDAP_PARSE_SORT_CONTROL if (!(ret = ldap_parse_sort_control( ld, controlU, &res, &attrU ))) { *result = res; *attr = strUtoW( attrU ); } #elif defined(HAVE_LDAP_PARSE_SORTRESPONSE_CONTROL) for (i = 0; controlU[i]; i++) { if (!strcmp( LDAP_SERVER_RESP_SORT_OID, controlU[i]->ldctl_oid )) sortcontrol = controlU[i]; } if (!sortcontrol) { controlarrayfreeU( controlU ); return WLDAP32_LDAP_CONTROL_NOT_FOUND; } if (!(ret = ldap_parse_sortresponse_control( ld, sortcontrol, &res, &attrU ))) { *result = res; *attr = strUtoW( attrU ); } #endif controlarrayfreeU( controlU ); #endif return map_error( ret ); }
int main( int argc, char **argv ) { LDAP *ld; LDAPMessage *result, *e; char *attrfail, *matched = NULL, *errmsg = NULL; char **vals, **referrals; int rc, parse_rc, version; unsigned long sortrc; LDAPControl *sortctrl = NULL; LDAPControl *requestctrls[ 2 ]; LDAPControl **resultctrls = NULL; LDAPsortkey **sortkeylist; /* Arrange for all connections to use LDAPv3 */ version = LDAP_VERSION3; if ( ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version ) != 0 ) { fprintf( stderr, "ldap_set_option protocol version to %d failed\n", version ); return( 1 ); } /* Get a handle to an LDAP connection */ if ( (ld = ldap_init( MY_HOST, MY_PORT ) ) == NULL ) { perror( "ldap_init" ); return( 1 ); } /* Authenticate as Directory Manager */ if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_simple_bind_s" ); ldap_unbind( ld ); return( 1 ); } /* * Create a sort key list that specifies the sort order of the results. * Sort the results by last name first, then by first name. */ ldap_create_sort_keylist( &sortkeylist, "description -givenname" ); /* Create the sort control. */ rc = ldap_create_sort_control( ld, sortkeylist, 1, &sortctrl ); if ( rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_create_sort_control: %s\n", ldap_err2string( rc ) ); ldap_unbind( ld ); return( 1 ); } requestctrls[ 0 ] = sortctrl; requestctrls[ 1 ] = NULL; /* Search for all entries in Sunnyvale */ rc = ldap_search_ext_s( ld, PEOPLE_BASE, LDAP_SCOPE_SUBTREE, "(objectclass=person)", NULL, 0, requestctrls, NULL, NULL, 0, &result ); if ( rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_search_ext_s: %s\n", ldap_err2string( rc ) ); ldap_unbind( ld ); return( 1 ); } parse_rc = ldap_parse_result( ld, result, &rc, &matched, &errmsg, &referrals, &resultctrls, 0 ); if ( parse_rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_parse_result: %s\n", ldap_err2string( parse_rc ) ); ldap_unbind( ld ); return( 1 ); } if ( rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_search_ext_s: %s\n", ldap_err2string( rc ) ); if ( errmsg != NULL && *errmsg != '\0' ) { fprintf( stderr, "%s\n", errmsg ); } ldap_unbind( ld ); return( 1 ); } parse_rc = ldap_parse_sort_control( ld, resultctrls, &sortrc, &attrfail ); if ( parse_rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_parse_sort_control: %s\n", ldap_err2string( parse_rc ) ); ldap_unbind( ld ); return( 1 ); } if ( sortrc != LDAP_SUCCESS ) { fprintf( stderr, "Sort error: %s\n", ldap_err2string( sortrc )); if ( attrfail != NULL && *attrfail != '\0' ) { fprintf( stderr, "Bad attribute: %s\n", attrfail); } ldap_unbind( ld ); return( 1 ); } /* for each entry print out name + all attrs and values */ for ( e = ldap_first_entry( ld, result ); e != NULL; e = ldap_next_entry( ld, e ) ) { if ((vals = ldap_get_values( ld, e, "sn")) != NULL ) { if ( vals[0] != NULL ) { printf( "%s", vals[0] ); } ldap_value_free( vals ); } if ((vals = ldap_get_values( ld, e, "givenname")) != NULL ) { if ( vals[0] != NULL ) { printf( "\t%s", vals[0] ); } ldap_value_free( vals ); } putchar( '\n' ); } ldap_msgfree( result ); ldap_free_sort_keylist( sortkeylist ); ldap_control_free( sortctrl ); ldap_controls_free( resultctrls ); ldap_unbind( ld ); return( 0 ); }