3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
17 #include <ac/unistd.h>
21 #ifdef HAVE_SYS_FILE_H
30 /* including the "internal" defs is legit and nec. since this test routine has
31 * a-priori knowledge of libldap internal workings.
32 * hodges@stanford.edu 5-Feb-96
38 static char *getline LDAP_P(( char *line, int len, FILE *fp, char *prompt ));
40 static char **get_list LDAP_P(( char *prompt ));
41 static int file_read LDAP_P(( char *path, struct berval *bv ));
42 static LDAPMod **get_modlist LDAP_P(( char *prompt1, char *prompt2, char *prompt3 ));
43 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
44 static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm, char *s ));
45 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
46 static void free_list LDAP_P(( char **list ));
48 #define NOCACHEERRMSG "don't compile with -DLDAP_NOCACHE if you desire local caching"
50 static char *dnsuffix;
54 getline( char *line, int len, FILE *fp, char *prompt )
58 if ( fgets( line, len, fp ) == NULL )
61 line[ strlen( line ) - 1 ] = '\0';
68 get_list( char *prompt )
77 getline( buf, sizeof(buf), stdin, prompt );
82 if ( result == (char **) 0 )
83 result = (char **) malloc( sizeof(char *) );
85 result = (char **) realloc( result,
86 sizeof(char *) * (num + 1) );
88 result[num++] = (char *) strdup( buf );
90 if ( result == (char **) 0 )
92 result = (char **) realloc( result, sizeof(char *) * (num + 1) );
100 free_list( char **list )
104 if ( list != NULL ) {
105 for ( i = 0; list[ i ] != NULL; ++i ) {
108 free( (char *)list );
114 file_read( char *path, struct berval *bv )
120 if (( fp = fopen( path, "r" )) == NULL ) {
125 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
131 bv->bv_len = ftell( fp );
133 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
139 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
145 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
149 if ( (ber_len_t) rlen != bv->bv_len ) {
155 return( bv->bv_len );
160 get_modlist( char *prompt1, char *prompt2, char *prompt3 )
162 static char buf[256];
166 struct berval **bvals;
172 getline( buf, sizeof(buf), stdin, prompt1 );
173 tmp.mod_op = atoi( buf );
175 if ( tmp.mod_op == -1 || buf[0] == '\0' )
179 getline( buf, sizeof(buf), stdin, prompt2 );
180 if ( buf[0] == '\0' )
182 tmp.mod_type = strdup( buf );
184 tmp.mod_values = get_list( prompt3 );
186 if ( tmp.mod_values != NULL ) {
189 for ( i = 0; tmp.mod_values[i] != NULL; ++i )
191 bvals = (struct berval **)calloc( i + 1,
192 sizeof( struct berval *));
193 for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
194 bvals[i] = (struct berval *)malloc(
195 sizeof( struct berval ));
196 if ( strncmp( tmp.mod_values[i], "{FILE}",
198 if ( file_read( tmp.mod_values[i] + 6,
203 bvals[i]->bv_val = tmp.mod_values[i];
205 strlen( tmp.mod_values[i] );
208 tmp.mod_bvalues = bvals;
209 tmp.mod_op |= LDAP_MOD_BVALUES;
212 if ( result == NULL )
213 result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
215 result = (LDAPMod **) realloc( result,
216 sizeof(LDAPMod *) * (num + 1) );
218 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
219 *(result[num]) = tmp; /* struct copy */
222 if ( result == NULL )
224 result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
232 bind_prompt( LDAP *ld, LDAP_CONST char *url, int request, ber_int_t msgid)
234 static char dn[256], passwd[256];
238 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
239 getline( dn, sizeof(dn), stdin,
240 "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
241 if (( authmethod = atoi( dn )) == 3 ) {
242 authmethod = LDAP_AUTH_KRBV4;
246 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
247 authmethod = LDAP_AUTH_SIMPLE;
248 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
250 getline( dn, sizeof(dn), stdin, "re-bind dn? " );
251 strcat( dn, dnsuffix );
253 if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
254 getline( passwd, sizeof(passwd), stdin,
255 "re-bind password? " );
260 return ldap_bind_s( ld, dn, passwd, authmethod);
265 main( int argc, char **argv )
268 int i, c, port, cldapflg, errflg, method, id, msgtype;
269 char line[256], command1, command2, command3;
270 char passwd[64], dn[256], rdn[64], attr[64], value[256];
271 char filter[256], *host, **types;
273 char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
274 int bound, all, scope, attrsonly;
276 LDAPMod **mods, **attrs;
277 struct timeval timeout;
278 char *copyfname = NULL;
285 cldapflg = errflg = 0;
287 while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
290 #ifdef LDAP_CONNECTIONLESS
292 #else /* LDAP_CONNECTIONLESS */
293 printf( "Compile with -DLDAP_CONNECTIONLESS for UDP support\n" );
294 #endif /* LDAP_CONNECTIONLESS */
299 ldap_debug = atoi( optarg );
301 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
302 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
306 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
319 port = atoi( optarg );
322 case 't': /* copy ber's to given file */
323 copyfname = strdup( optarg );
324 copyoptions = LBER_TO_FILE;
327 case 'T': /* only output ber's to given file */
328 copyfname = strdup( optarg );
329 copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
337 if ( host == NULL && optind == argc - 1 ) {
338 host = argv[ optind ];
342 if ( errflg || optind < argc - 1 ) {
343 fprintf( stderr, usage, argv[ 0 ] );
344 exit( EXIT_FAILURE );
347 printf( "%s( %s, %d )\n",
348 cldapflg ? "cldap_open" : "ldap_init",
349 host == NULL ? "(null)" : host, port );
352 #ifdef LDAP_CONNECTIONLESS
353 ld = cldap_open( host, port );
354 #endif /* LDAP_CONNECTIONLESS */
356 ld = ldap_init( host, port );
360 perror( cldapflg ? "cldap_open" : "ldap_init" );
361 exit( EXIT_FAILURE );
364 if ( copyfname != NULL ) {
365 if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
368 exit ( EXIT_FAILURE );
370 ld->ld_sb.sb_options = copyoptions;
377 (void) memset( line, '\0', sizeof(line) );
378 while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
383 switch ( command1 ) {
384 case 'a': /* add or abandon */
385 switch ( command2 ) {
387 getline( dn, sizeof(dn), stdin, "dn? " );
388 strcat( dn, dnsuffix );
389 if ( (attrs = get_modlist( NULL, "attr? ",
390 "value? " )) == NULL )
392 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
393 ldap_perror( ld, "ldap_add" );
395 printf( "Add initiated with id %d\n",
399 case 'b': /* abandon */
400 getline( line, sizeof(line), stdin, "msgid? " );
402 if ( ldap_abandon( ld, id ) != 0 )
403 ldap_perror( ld, "ldap_abandon" );
405 printf( "Abandon successful\n" );
408 printf( "Possibilities: [ad]d, [ab]ort\n" );
412 case 'b': /* asynch bind */
413 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
414 getline( line, sizeof(line), stdin,
415 "method (0->simple, 1->krbv41, 2->krbv42)? " );
416 method = atoi( line ) | 0x80;
417 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
418 method = LDAP_AUTH_SIMPLE;
419 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
420 getline( dn, sizeof(dn), stdin, "dn? " );
421 strcat( dn, dnsuffix );
423 if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
424 getline( passwd, sizeof(passwd), stdin,
429 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
430 fprintf( stderr, "ldap_bind failed\n" );
431 ldap_perror( ld, "ldap_bind" );
433 printf( "Bind initiated\n" );
438 case 'B': /* synch bind */
439 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
440 getline( line, sizeof(line), stdin,
441 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
442 method = atoi( line );
444 method = LDAP_AUTH_KRBV4;
446 method = method | 0x80;
447 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
448 method = LDAP_AUTH_SIMPLE;
449 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
450 getline( dn, sizeof(dn), stdin, "dn? " );
451 strcat( dn, dnsuffix );
454 getline( passwd, sizeof(passwd), stdin,
459 if ( ldap_bind_s( ld, dn, passwd, method ) !=
461 fprintf( stderr, "ldap_bind_s failed\n" );
462 ldap_perror( ld, "ldap_bind_s" );
464 printf( "Bind successful\n" );
469 case 'c': /* compare */
470 getline( dn, sizeof(dn), stdin, "dn? " );
471 strcat( dn, dnsuffix );
472 getline( attr, sizeof(attr), stdin, "attr? " );
473 getline( value, sizeof(value), stdin, "value? " );
475 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
476 ldap_perror( ld, "ldap_compare" );
478 printf( "Compare initiated with id %d\n", id );
481 case 'd': /* turn on debugging */
483 getline( line, sizeof(line), stdin, "debug level? " );
484 ldap_debug = atoi( line );
486 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
487 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
491 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
495 case 'E': /* explode a dn */
496 getline( line, sizeof(line), stdin, "dn? " );
497 exdn = ldap_explode_dn( line, 0 );
498 for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
499 printf( "\t%s\n", exdn[i] );
503 case 'g': /* set next msgid */
504 getline( line, sizeof(line), stdin, "msgid? " );
505 ld->ld_msgid = atoi( line );
508 case 'v': /* set version number */
509 getline( line, sizeof(line), stdin, "version? " );
510 ld->ld_version = atoi( line );
513 case 'm': /* modify or modifyrdn */
514 if ( strncmp( line, "modify", 4 ) == 0 ) {
515 getline( dn, sizeof(dn), stdin, "dn? " );
516 strcat( dn, dnsuffix );
517 if ( (mods = get_modlist(
518 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
519 "attribute type? ", "attribute value? " ))
522 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
523 ldap_perror( ld, "ldap_modify" );
525 printf( "Modify initiated with id %d\n",
527 } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
528 getline( dn, sizeof(dn), stdin, "dn? " );
529 strcat( dn, dnsuffix );
530 getline( rdn, sizeof(rdn), stdin, "newrdn? " );
531 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
532 ldap_perror( ld, "ldap_modrdn" );
534 printf( "Modrdn initiated with id %d\n",
537 printf( "Possibilities: [modi]fy, [modr]dn\n" );
542 #ifdef LDAP_CONNECTIONLESS
545 #endif /* LDAP_CONNECTIONLESS */
550 exit( EXIT_SUCCESS );
553 case 'r': /* result or remove */
554 switch ( command3 ) {
555 case 's': /* result */
556 getline( line, sizeof(line), stdin,
557 "msgid (-1=>any)? " );
558 if ( line[0] == '\0' )
562 getline( line, sizeof(line), stdin,
563 "all (0=>any, 1=>all)? " );
564 if ( line[0] == '\0' )
568 if (( msgtype = ldap_result( ld, id, all,
569 &timeout, &res )) < 1 ) {
570 ldap_perror( ld, "ldap_result" );
573 printf( "\nresult: msgtype %d msgid %d\n",
574 msgtype, res->lm_msgid );
575 handle_result( ld, res );
579 case 'm': /* remove */
580 getline( dn, sizeof(dn), stdin, "dn? " );
581 strcat( dn, dnsuffix );
582 if ( (id = ldap_delete( ld, dn )) == -1 )
583 ldap_perror( ld, "ldap_delete" );
585 printf( "Remove initiated with id %d\n",
590 printf( "Possibilities: [rem]ove, [res]ult\n" );
595 case 's': /* search */
596 getline( dn, sizeof(dn), stdin, "searchbase? " );
597 strcat( dn, dnsuffix );
598 getline( line, sizeof(line), stdin,
599 "scope (0=Base, 1=One Level, 2=Subtree)? " );
600 scope = atoi( line );
601 getline( filter, sizeof(filter), stdin,
602 "search filter (e.g. sn=jones)? " );
603 types = get_list( "attrs to return? " );
604 getline( line, sizeof(line), stdin,
605 "attrsonly (0=attrs&values, 1=attrs only)? " );
606 attrsonly = atoi( line );
609 #ifdef LDAP_CONNECTIONLESS
610 getline( line, sizeof(line), stdin,
611 "Requestor DN (for logging)? " );
612 if ( cldap_search_s( ld, dn, scope, filter, types,
613 attrsonly, &res, line ) != 0 ) {
614 ldap_perror( ld, "cldap_search_s" );
616 printf( "\nresult: msgid %d\n",
618 handle_result( ld, res );
621 #endif /* LDAP_CONNECTIONLESS */
623 if (( id = ldap_search( ld, dn, scope, filter,
624 types, attrsonly )) == -1 ) {
625 ldap_perror( ld, "ldap_search" );
627 printf( "Search initiated with id %d\n", id );
633 case 't': /* set timeout value */
634 getline( line, sizeof(line), stdin, "timeout? " );
635 timeout.tv_sec = atoi( line );
638 case 'U': /* set ufn search prefix */
639 getline( line, sizeof(line), stdin, "ufn prefix? " );
640 ldap_ufn_setprefix( ld, line );
643 case 'u': /* user friendly search w/optional timeout */
644 getline( dn, sizeof(dn), stdin, "ufn? " );
645 strcat( dn, dnsuffix );
646 types = get_list( "attrs to return? " );
647 getline( line, sizeof(line), stdin,
648 "attrsonly (0=attrs&values, 1=attrs only)? " );
649 attrsonly = atoi( line );
651 if ( command2 == 't' ) {
652 id = ldap_ufn_search_c( ld, dn, types,
653 attrsonly, &res, ldap_ufn_timeout,
656 id = ldap_ufn_search_s( ld, dn, types,
660 ldap_perror( ld, "ldap_ufn_search" );
662 printf( "\nresult: err %d\n", id );
663 handle_result( ld, res );
669 case 'l': /* URL search */
670 getline( line, sizeof(line), stdin,
671 "attrsonly (0=attrs&values, 1=attrs only)? " );
672 attrsonly = atoi( line );
673 getline( line, sizeof(line), stdin, "LDAP URL? " );
674 if (( id = ldap_url_search( ld, line, attrsonly ))
676 ldap_perror( ld, "ldap_url_search" );
678 printf( "URL search initiated with id %d\n", id );
682 case 'p': /* parse LDAP URL */
683 getline( line, sizeof(line), stdin, "LDAP URL? " );
684 if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
685 fprintf( stderr, "ldap_url_parse: error %d\n", i );
687 printf( "\t host: " );
688 if ( ludp->lud_host == NULL ) {
689 printf( "DEFAULT\n" );
691 printf( "<%s>\n", ludp->lud_host );
693 printf( "\t port: " );
694 if ( ludp->lud_port == 0 ) {
695 printf( "DEFAULT\n" );
697 printf( "%d\n", ludp->lud_port );
699 printf( "\t dn: <%s>\n", ludp->lud_dn );
700 printf( "\t attrs:" );
701 if ( ludp->lud_attrs == NULL ) {
704 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
705 printf( " <%s>", ludp->lud_attrs[ i ] );
708 printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
709 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
710 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
711 printf( "\tfilter: <%s>\n", ludp->lud_filter );
712 ldap_free_urldesc( ludp );
716 case 'n': /* set dn suffix, for convenience */
717 getline( line, sizeof(line), stdin, "DN suffix? " );
718 strcpy( dnsuffix, line );
721 case 'e': /* enable cache */
723 printf( NOCACHEERRMSG );
724 #else /* LDAP_NOCACHE */
725 getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
727 getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
728 if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
729 printf( "local cache is on\n" );
731 printf( "ldap_enable_cache failed\n" );
733 #endif /* LDAP_NOCACHE */
736 case 'x': /* uncache entry */
738 printf( NOCACHEERRMSG );
739 #else /* LDAP_NOCACHE */
740 getline( line, sizeof(line), stdin, "DN? " );
741 ldap_uncache_entry( ld, line );
742 #endif /* LDAP_NOCACHE */
745 case 'X': /* uncache request */
747 printf( NOCACHEERRMSG );
748 #else /* LDAP_NOCACHE */
749 getline( line, sizeof(line), stdin, "request msgid? " );
750 ldap_uncache_request( ld, atoi( line ));
751 #endif /* LDAP_NOCACHE */
754 case 'o': /* set ldap options */
755 getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
756 ld->ld_deref = atoi( line );
757 getline( line, sizeof(line), stdin, "timelimit?" );
758 ld->ld_timelimit = atoi( line );
759 getline( line, sizeof(line), stdin, "sizelimit?" );
760 ld->ld_sizelimit = atoi( line );
762 LDAP_BOOL_ZERO(&ld->ld_options);
764 #ifdef STR_TRANSLATION
765 getline( line, sizeof(line), stdin,
766 "Automatic translation of T.61 strings (0=no, 1=yes)?" );
767 if ( atoi( line ) == 0 ) {
768 ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
770 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
771 #ifdef LDAP_CHARSET_8859
772 getline( line, sizeof(line), stdin,
773 "Translate to/from ISO-8859 (0=no, 1=yes?" );
774 if ( atoi( line ) != 0 ) {
775 ldap_set_string_translators( ld,
779 #endif /* LDAP_CHARSET_8859 */
781 #endif /* STR_TRANSLATION */
783 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
784 getline( line, sizeof(line), stdin,
785 "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
786 if ( atoi( line ) != 0 ) {
787 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS);
789 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
791 getline( line, sizeof(line), stdin,
792 "Recognize and chase referrals (0=no, 1=yes)?" );
793 if ( atoi( line ) != 0 ) {
794 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
795 getline( line, sizeof(line), stdin,
796 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
797 if ( atoi( line ) != 0 ) {
798 ldap_set_rebind_proc( ld, bind_prompt );
803 case 'O': /* set cache options */
805 printf( NOCACHEERRMSG );
806 #else /* LDAP_NOCACHE */
807 getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
808 switch( atoi( line )) {
810 ldap_set_cache_options( ld, 0 );
813 ldap_set_cache_options( ld,
814 LDAP_CACHE_OPT_CACHENOERRS );
817 ldap_set_cache_options( ld,
818 LDAP_CACHE_OPT_CACHEALLERRS );
821 printf( "not a valid cache option\n" );
823 #endif /* LDAP_NOCACHE */
827 printf( "Commands: [ad]d [ab]andon [b]ind\n" );
828 printf( " [B]ind async [c]ompare [l]URL search\n" );
829 printf( " [modi]fy [modr]dn [rem]ove\n" );
830 printf( " [res]ult [s]earch [q]uit/unbind\n\n" );
831 printf( " [u]fn search [ut]fn search with timeout\n" );
832 printf( " [d]ebug [e]nable cache set ms[g]id\n" );
833 printf( " d[n]suffix [t]imeout [v]ersion\n" );
834 printf( " [U]fn prefix [x]uncache entry [X]uncache request\n" );
835 printf( " [?]help [o]ptions [O]cache options\n" );
836 printf( " [E]xplode dn [p]arse LDAP URL\n" );
840 printf( "Invalid command. Type ? for help.\n" );
844 (void) memset( line, '\0', sizeof(line) );
851 handle_result( LDAP *ld, LDAPMessage *lm )
853 switch ( lm->lm_msgtype ) {
854 case LDAP_RES_COMPARE:
855 printf( "Compare result\n" );
856 print_ldap_result( ld, lm, "compare" );
859 case LDAP_RES_SEARCH_RESULT:
860 printf( "Search result\n" );
861 print_ldap_result( ld, lm, "search" );
864 case LDAP_RES_SEARCH_ENTRY:
865 printf( "Search entry\n" );
866 print_search_entry( ld, lm );
870 printf( "Add result\n" );
871 print_ldap_result( ld, lm, "add" );
874 case LDAP_RES_DELETE:
875 printf( "Delete result\n" );
876 print_ldap_result( ld, lm, "delete" );
879 case LDAP_RES_MODRDN:
880 printf( "ModRDN result\n" );
881 print_ldap_result( ld, lm, "modrdn" );
885 printf( "Bind result\n" );
886 print_ldap_result( ld, lm, "bind" );
890 printf( "Unknown result type 0x%lx\n",
891 (unsigned long) lm->lm_msgtype );
892 print_ldap_result( ld, lm, "unknown" );
897 print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
899 ldap_result2error( ld, lm, 1 );
900 ldap_perror( ld, s );
902 if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
903 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
904 if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
905 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
910 print_search_entry( LDAP *ld, LDAPMessage *res )
914 for ( e = ldap_first_entry( ld, res ); e != NULL;
915 e = ldap_next_entry( ld, e ) )
917 BerElement *ber = NULL;
920 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
923 dn = ldap_get_dn( ld, e );
924 printf( "\tDN: %s\n", dn );
926 ufn = ldap_dn2ufn( dn );
927 printf( "\tUFN: %s\n", ufn );
932 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
933 a = ldap_next_attribute( ld, e, ber ) )
935 struct berval **vals;
937 printf( "\t\tATTR: %s\n", a );
938 if ( (vals = ldap_get_values_len( ld, e, a ))
940 printf( "\t\t\t(no values)\n" );
943 for ( i = 0; vals[i] != NULL; i++ ) {
947 for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
948 if ( !isascii( vals[i]->bv_val[j] ) ) {
954 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
955 #ifdef BPRINT_NONASCII
956 ber_bprint( vals[i]->bv_val,
958 #endif /* BPRINT_NONASCII */
961 printf( "\t\t\tlength (%ld) %s\n",
962 vals[i]->bv_len, vals[i]->bv_val );
964 ber_bvecfree( vals );
973 if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
974 || res->lm_chain != NULL )
975 print_ldap_result( ld, res, "search" );