From 0ad86cb23ad5a115f5b16649636c898060ac8292 Mon Sep 17 00:00:00 2001 From: Quanah Gibson-Mount Date: Wed, 3 Sep 2008 01:29:48 +0000 Subject: [PATCH] Add server side sorting --- clients/tools/common.c | 22 ++++++++++++++++ clients/tools/ldapsearch.c | 53 ++++++++++++++++++++++++++++++++++++++ doc/man/man1/ldapsearch.1 | 1 + 3 files changed, 76 insertions(+) diff --git a/clients/tools/common.c b/clients/tools/common.c index c5e3e9db42..69ab796c3e 100644 --- a/clients/tools/common.c +++ b/clients/tools/common.c @@ -134,6 +134,7 @@ static int print_paged_results( LDAP *ld, LDAPControl *ctrl ); #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST static int print_ppolicy( LDAP *ld, LDAPControl *ctrl ); #endif +static int print_sss( LDAP *ld, LDAPControl *ctrl ); static struct tool_ctrls_t { const char *oid; @@ -146,6 +147,7 @@ static struct tool_ctrls_t { #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST { LDAP_CONTROL_PASSWORDPOLICYRESPONSE, TOOL_ALL, print_ppolicy }, #endif + { LDAP_CONTROL_SORTRESPONSE, TOOL_SEARCH, print_sss }, { NULL, 0, NULL } }; @@ -1864,6 +1866,26 @@ print_paged_results( LDAP *ld, LDAPControl *ctrl ) return 0; } +static int +print_sss( LDAP *ld, LDAPControl *ctrl ) +{ + int rc; + ber_int_t err; + char *attr; + + rc = ldap_parse_sortresponse_control( ld, ctrl, &err, &attr ); + if ( rc == LDAP_SUCCESS ) { + char buf[ BUFSIZ ]; + rc = snprintf( buf, sizeof(buf), "(%d) %s %s", + err, ldap_err2string(err), attr ? attr : "" ); + + tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE, + "sortResult", buf, rc ); + } + + return rc; +} + #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST static int print_ppolicy( LDAP *ld, LDAPControl *ctrl ) diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c index d570036298..aeb56d036a 100644 --- a/clients/tools/ldapsearch.c +++ b/clients/tools/ldapsearch.c @@ -128,6 +128,7 @@ usage( void ) fprintf( stderr, _(" !dontUseCopy (Don't Use Copy)\n")); fprintf( stderr, _(" [!]mv= (matched values filter)\n")); fprintf( stderr, _(" [!]pr=[/prompt|noprompt] (paged results/prompt)\n")); + fprintf( stderr, _(" [!]sss=[-][/[-]...] (server side sorting)\n")); fprintf( stderr, _(" [!]subentries[=true|false] (subentries)\n")); fprintf( stderr, _(" [!]sync=ro[/] (LDAP Sync refreshOnly)\n")); fprintf( stderr, _(" rp[/][/] (LDAP Sync refreshAndPersist)\n")); @@ -199,6 +200,9 @@ static int dontUseCopy = 0; static int domainScope = 0; +static int sss = 0; +static LDAPSortKey **sss_keys = NULL; + static int ldapsync = 0; static struct berval sync_cookie = { 0, NULL }; static int sync_slimit = -1; @@ -395,6 +399,31 @@ handle_private_option( int i ) domainScope = 1 + crit; + } else if ( strcasecmp( control, "sss" ) == 0 ) { + char *keyp; + if( sss ) { + fprintf( stderr, + _("server side sorting control previously specified\n")); + exit( EXIT_FAILURE ); + } + if( cvalue == NULL ) { + fprintf( stderr, + _("missing specification of sss control\n") ); + exit( EXIT_FAILURE ); + } + keyp = cvalue; + while (keyp = strchr(keyp, '/')) { + *keyp++ = ' '; + } + if ( ldap_create_sort_keylist( &sss_keys, cvalue )) { + fprintf( stderr, + _("server side sorting control value \"%s\" invalid\n"), + cvalue ); + exit( EXIT_FAILURE ); + } + + sss = 1 + crit; + } else if ( strcasecmp( control, "subentries" ) == 0 ) { if( subentries ) { fprintf( stderr, @@ -754,6 +783,7 @@ getNextPage: || domainScope || pagedResults || ldapsync + || sss || subentries || valuesReturnFilter ) { @@ -886,6 +916,22 @@ getNextPage: c[i].ldctl_iscritical = pagedResults > 1; i++; } + + if ( sss ) { + if ( ctrl_add() ) { + return EXIT_FAILURE; + } + + if ( ldap_create_sort_control_value( ld, + sss_keys, &c[i].ldctl_value ) ) + { + return EXIT_FAILURE; + } + + c[i].ldctl_oid = LDAP_CONTROL_SORTREQUEST; + c[i].ldctl_iscritical = sss > 1; + i++; + } } tool_server_controls( ld, c, i ); @@ -968,6 +1014,10 @@ getNextPage: (pagedResults > 1) ? _("critical ") : "", pageSize ); } + if ( sss ) { + printf(_("\n# with server side sorting %scontrol"), + sss > 1 ? _("critical ") : "" ); + } printf( _("\n#\n\n") ); @@ -1051,6 +1101,9 @@ getNextPage: if ( control != NULL ) { ber_memfree( control ); } + if ( sss_keys != NULL ) { + ldap_free_sort_keylist( sss_keys ); + } if ( c ) { for ( ; save_nctrls-- > 0; ) { diff --git a/doc/man/man1/ldapsearch.1 b/doc/man/man1/ldapsearch.1 index 19170003d0..6d203d6418 100644 --- a/doc/man/man1/ldapsearch.1 +++ b/doc/man/man1/ldapsearch.1 @@ -274,6 +274,7 @@ Search extensions: [!]domainScope (domain scope) [!]mv= (matched values filter) [!]pr=[/prompt|noprompt] (paged results/prompt) + [!]sss=[-][/[-]...] (server side sorting) [!]subentries[=true|false] (subentries) [!]sync=ro[/] (LDAP Sync refreshOnly) rp[/][/] (LDAP Sync refreshAndPersist) -- 2.39.5