-
-static void print_ctrls(
- LDAPControl **ctrls )
-{
- int i;
- for(i=0; ctrls[i] != NULL; i++ ) {
- /* control: OID criticality base64value */
- struct berval *b64 = NULL;
- ber_len_t len;
- char *str;
-
- len = ldif ? 2 : 0;
- len += strlen( ctrls[i]->ldctl_oid );
-
- /* add enough for space after OID and the critical value itself */
- len += ctrls[i]->ldctl_iscritical
- ? sizeof("true") : sizeof("false");
-
- /* convert to base64 */
- if( ctrls[i]->ldctl_value.bv_len ) {
- b64 = ber_memalloc( sizeof(struct berval) );
-
- b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
- ctrls[i]->ldctl_value.bv_len ) + 1;
- b64->bv_val = ber_memalloc( b64->bv_len + 1 );
-
- b64->bv_len = lutil_b64_ntop(
- (unsigned char *) ctrls[i]->ldctl_value.bv_val,
- ctrls[i]->ldctl_value.bv_len,
- b64->bv_val, b64->bv_len );
- }
-
- if( b64 ) {
- len += 1 + b64->bv_len;
- }
-
- str = malloc( len + 1 );
- if ( ldif ) {
- strcpy( str, ": " );
- } else {
- str[0] = '\0';
- }
- strcat( str, ctrls[i]->ldctl_oid );
- strcat( str, ctrls[i]->ldctl_iscritical
- ? " true" : " false" );
-
- if( b64 ) {
- strcat(str, " ");
- strcat(str, b64->bv_val );
- }
-
- if ( ldif < 2 ) {
- write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
- "control", str, len );
- }
-
- free( str );
- ber_bvfree( b64 );
- }
-}
-
-static int
-write_ldif( int type, char *name, char *value, ber_len_t vallen )
-{
- char *ldif;
-
- if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
- return( -1 );
- }
-
- fputs( ldif, stdout );
- ber_memfree( ldif );
-
- return( 0 );
-}
-
-
-#ifdef LDAP_CONTROL_PAGEDRESULTS
-static int
-parse_page_control(
- LDAP *ld,
- LDAPMessage *result,
- struct berval *cookie )
-{
- int rc;
- int err;
- LDAPControl **ctrl = NULL;
- LDAPControl *ctrlp = NULL;
- BerElement *ber;
- ber_tag_t tag;
-
- rc = ldap_parse_result( ld, result,
- &err, NULL, NULL, NULL, &ctrl, 0 );
-
- if( rc != LDAP_SUCCESS ) {
- tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
- exit( EXIT_FAILURE );
- }
-
- if ( err != LDAP_SUCCESS ) {
- fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
- }
-
- if( ctrl ) {
- /* Parse the control value
- * searchResult ::= SEQUENCE {
- * size INTEGER (0..maxInt),
- * -- result set size estimate from server - unused
- * cookie OCTET STRING
- * }
- */
- ctrlp = *ctrl;
- ber = ber_init( &ctrlp->ldctl_value );
- if ( ber == NULL ) {
- fprintf( stderr, _("Internal error.\n") );
- return EXIT_FAILURE;
- }
-
- tag = ber_scanf( ber, "{io}", &entriesLeft, cookie );
- (void) ber_free( ber, 1 );
-
- if( tag == LBER_ERROR ) {
- fprintf( stderr,
- _("Paged results response control could not be decoded.\n") );
- return EXIT_FAILURE;
- }
-
- if( entriesLeft < 0 ) {
- fprintf( stderr,
- _("Invalid entries estimate in paged results response.\n") );
- return EXIT_FAILURE;
- }
-
- if ( cookie->bv_len == 0 ) {
- morePagedResults = 0;
- }
-
- ldap_controls_free( ctrl );
-
- } else {
- morePagedResults = 0;
- }
-
- return err;
-}
-#endif