]> git.sur5r.net Git - openldap/commitdiff
Add server side sorting
authorQuanah Gibson-Mount <quanah@openldap.org>
Wed, 3 Sep 2008 01:29:48 +0000 (01:29 +0000)
committerQuanah Gibson-Mount <quanah@openldap.org>
Wed, 3 Sep 2008 01:29:48 +0000 (01:29 +0000)
clients/tools/common.c
clients/tools/ldapsearch.c
doc/man/man1/ldapsearch.1

index c5e3e9db4276beef720d281a832f5fad9a1d0035..69ab796c3ea3284e5ba2151cc73aadf30f4773b8 100644 (file)
@@ -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 )
index d5700362989de0c7a4db7b8a9710c533a205aba2..aeb56d036ad20ea80e85d1d2d21b1fb0e6fc68eb 100644 (file)
@@ -128,6 +128,7 @@ usage( void )
        fprintf( stderr, _("             !dontUseCopy                (Don't Use Copy)\n"));
        fprintf( stderr, _("             [!]mv=<filter>              (matched values filter)\n"));
        fprintf( stderr, _("             [!]pr=<size>[/prompt|noprompt]   (paged results/prompt)\n"));
+       fprintf( stderr, _("             [!]sss=[-]<attr[:OID]>[/[-]<attr[:OID]>...]   (server side sorting)\n"));
        fprintf( stderr, _("             [!]subentries[=true|false]  (subentries)\n"));
        fprintf( stderr, _("             [!]sync=ro[/<cookie>]            (LDAP Sync refreshOnly)\n"));
        fprintf( stderr, _("                     rp[/<cookie>][/<slimit>] (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; ) {
index 19170003d0b251bb93a8e1685df6d77a9bca2c48..6d203d641827bae7e37e5fdc01f39f484ed78958 100644 (file)
@@ -274,6 +274,7 @@ Search extensions:
   [!]domainScope                               (domain scope)
   [!]mv=<filter>                               (matched values filter)
   [!]pr=<size>[/prompt|noprompt]       (paged results/prompt)
+  [!]sss=[-]<attr[:OID]>[/[-]<attr[:OID]>...]  (server side sorting)
   [!]subentries[=true|false]           (subentries)
   [!]sync=ro[/<cookie>]                        (LDAP Sync refreshOnly)
           rp[/<cookie>][/<slimit>]     (LDAP Sync refreshAndPersist)