3 * Copyright 1998-2002 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, const char *prompt ));
40 static char **get_list LDAP_P(( const char *prompt ));
41 static int file_read LDAP_P(( const char *path, struct berval *bv ));
42 static LDAPMod **get_modlist LDAP_P(( const char *prompt1,
43 const char *prompt2, const char *prompt3 ));
44 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
45 static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm,
47 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
48 static void free_list LDAP_P(( char **list ));
50 #define NOCACHEERRMSG "don't compile with -DLDAP_NOCACHE if you desire local caching"
52 static char *dnsuffix;
56 getline( char *line, int len, FILE *fp, const char *prompt )
60 if ( fgets( line, len, fp ) == NULL )
63 line[ strlen( line ) - 1 ] = '\0';
70 get_list( const char *prompt )
79 getline( buf, sizeof(buf), stdin, prompt );
84 if ( result == (char **) 0 )
85 result = (char **) malloc( sizeof(char *) );
87 result = (char **) realloc( result,
88 sizeof(char *) * (num + 1) );
90 result[num++] = (char *) strdup( buf );
92 if ( result == (char **) 0 )
94 result = (char **) realloc( result, sizeof(char *) * (num + 1) );
102 free_list( char **list )
106 if ( list != NULL ) {
107 for ( i = 0; list[ i ] != NULL; ++i ) {
110 free( (char *)list );
116 file_read( const char *path, struct berval *bv )
122 if (( fp = fopen( path, "r" )) == NULL ) {
127 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
133 bv->bv_len = ftell( fp );
135 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
141 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
147 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
151 if ( (ber_len_t) rlen != bv->bv_len ) {
157 return( bv->bv_len );
165 const char *prompt3 )
167 static char buf[256];
171 struct berval **bvals;
177 getline( buf, sizeof(buf), stdin, prompt1 );
178 tmp.mod_op = atoi( buf );
180 if ( tmp.mod_op == -1 || buf[0] == '\0' )
184 getline( buf, sizeof(buf), stdin, prompt2 );
185 if ( buf[0] == '\0' )
187 tmp.mod_type = strdup( buf );
189 tmp.mod_values = get_list( prompt3 );
191 if ( tmp.mod_values != NULL ) {
194 for ( i = 0; tmp.mod_values[i] != NULL; ++i )
196 bvals = (struct berval **)calloc( i + 1,
197 sizeof( struct berval *));
198 for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
199 bvals[i] = (struct berval *)malloc(
200 sizeof( struct berval ));
201 if ( strncmp( tmp.mod_values[i], "{FILE}",
203 if ( file_read( tmp.mod_values[i] + 6,
208 bvals[i]->bv_val = tmp.mod_values[i];
210 strlen( tmp.mod_values[i] );
213 tmp.mod_bvalues = bvals;
214 tmp.mod_op |= LDAP_MOD_BVALUES;
217 if ( result == NULL )
218 result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
220 result = (LDAPMod **) realloc( result,
221 sizeof(LDAPMod *) * (num + 1) );
223 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
224 *(result[num]) = tmp; /* struct copy */
227 if ( result == NULL )
229 result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
237 bind_prompt( LDAP *ld,
238 LDAP_CONST char *url,
239 ber_tag_t request, ber_int_t msgid,
242 static char dn[256], passwd[256];
245 printf("rebind for request=%ld msgid=%ld url=%s\n",
246 request, (long) msgid, url );
248 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
249 getline( dn, sizeof(dn), stdin,
250 "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
251 if (( authmethod = atoi( dn )) == 3 ) {
252 authmethod = LDAP_AUTH_KRBV4;
256 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
257 authmethod = LDAP_AUTH_SIMPLE;
258 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
260 getline( dn, sizeof(dn), stdin, "re-bind dn? " );
261 strcat( dn, dnsuffix );
263 if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
264 getline( passwd, sizeof(passwd), stdin,
265 "re-bind password? " );
270 return ldap_bind_s( ld, dn, passwd, authmethod);
275 main( int argc, char **argv )
278 int i, c, port, errflg, method, id, msgtype;
279 char line[256], command1, command2, command3;
280 char passwd[64], dn[256], rdn[64], attr[64], value[256];
281 char filter[256], *host, **types;
283 char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
284 int bound, all, scope, attrsonly;
286 LDAPMod **mods, **attrs;
287 struct timeval timeout;
288 char *copyfname = NULL;
297 while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
301 ldap_debug = atoi( optarg );
303 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
304 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
308 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
321 port = atoi( optarg );
324 case 't': /* copy ber's to given file */
325 copyfname = strdup( optarg );
326 /* copyoptions = LBER_TO_FILE; */
329 case 'T': /* only output ber's to given file */
330 copyfname = strdup( optarg );
331 /* copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
339 if ( host == NULL && optind == argc - 1 ) {
340 host = argv[ optind ];
344 if ( errflg || optind < argc - 1 ) {
345 fprintf( stderr, usage, argv[ 0 ] );
346 exit( EXIT_FAILURE );
349 printf( "ldap_init( %s, %d )\n",
350 host == NULL ? "(null)" : host, port );
352 ld = ldap_init( host, port );
355 perror( "ldap_init" );
356 exit( EXIT_FAILURE );
359 if ( copyfname != NULL ) {
360 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
363 exit ( EXIT_FAILURE );
365 ld->ld_sb->sb_options = copyoptions;
372 (void) memset( line, '\0', sizeof(line) );
373 while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
378 switch ( command1 ) {
379 case 'a': /* add or abandon */
380 switch ( command2 ) {
382 getline( dn, sizeof(dn), stdin, "dn? " );
383 strcat( dn, dnsuffix );
384 if ( (attrs = get_modlist( NULL, "attr? ",
385 "value? " )) == NULL )
387 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
388 ldap_perror( ld, "ldap_add" );
390 printf( "Add initiated with id %d\n",
394 case 'b': /* abandon */
395 getline( line, sizeof(line), stdin, "msgid? " );
397 if ( ldap_abandon( ld, id ) != 0 )
398 ldap_perror( ld, "ldap_abandon" );
400 printf( "Abandon successful\n" );
403 printf( "Possibilities: [ad]d, [ab]ort\n" );
407 case 'b': /* asynch bind */
408 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
409 getline( line, sizeof(line), stdin,
410 "method (0->simple, 1->krbv41, 2->krbv42)? " );
411 method = atoi( line ) | 0x80;
412 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
413 method = LDAP_AUTH_SIMPLE;
414 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
415 getline( dn, sizeof(dn), stdin, "dn? " );
416 strcat( dn, dnsuffix );
418 if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
419 getline( passwd, sizeof(passwd), stdin,
424 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
425 fprintf( stderr, "ldap_bind failed\n" );
426 ldap_perror( ld, "ldap_bind" );
428 printf( "Bind initiated\n" );
433 case 'B': /* synch bind */
434 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
435 getline( line, sizeof(line), stdin,
436 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
437 method = atoi( line );
439 method = LDAP_AUTH_KRBV4;
441 method = method | 0x80;
442 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
443 method = LDAP_AUTH_SIMPLE;
444 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
445 getline( dn, sizeof(dn), stdin, "dn? " );
446 strcat( dn, dnsuffix );
449 getline( passwd, sizeof(passwd), stdin,
454 if ( ldap_bind_s( ld, dn, passwd, method ) !=
456 fprintf( stderr, "ldap_bind_s failed\n" );
457 ldap_perror( ld, "ldap_bind_s" );
459 printf( "Bind successful\n" );
464 case 'c': /* compare */
465 getline( dn, sizeof(dn), stdin, "dn? " );
466 strcat( dn, dnsuffix );
467 getline( attr, sizeof(attr), stdin, "attr? " );
468 getline( value, sizeof(value), stdin, "value? " );
470 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
471 ldap_perror( ld, "ldap_compare" );
473 printf( "Compare initiated with id %d\n", id );
476 case 'd': /* turn on debugging */
478 getline( line, sizeof(line), stdin, "debug level? " );
479 ldap_debug = atoi( line );
481 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
482 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
486 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
490 case 'E': /* explode a dn */
491 getline( line, sizeof(line), stdin, "dn? " );
492 exdn = ldap_explode_dn( line, 0 );
493 for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
494 printf( "\t%s\n", exdn[i] );
498 case 'g': /* set next msgid */
499 getline( line, sizeof(line), stdin, "msgid? " );
500 ld->ld_msgid = atoi( line );
503 case 'v': /* set version number */
504 getline( line, sizeof(line), stdin, "version? " );
505 ld->ld_version = atoi( line );
508 case 'm': /* modify or modifyrdn */
509 if ( strncmp( line, "modify", 4 ) == 0 ) {
510 getline( dn, sizeof(dn), stdin, "dn? " );
511 strcat( dn, dnsuffix );
512 if ( (mods = get_modlist(
513 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
514 "attribute type? ", "attribute value? " ))
517 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
518 ldap_perror( ld, "ldap_modify" );
520 printf( "Modify initiated with id %d\n",
522 } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
523 getline( dn, sizeof(dn), stdin, "dn? " );
524 strcat( dn, dnsuffix );
525 getline( rdn, sizeof(rdn), stdin, "newrdn? " );
526 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
527 ldap_perror( ld, "ldap_modrdn" );
529 printf( "Modrdn initiated with id %d\n",
532 printf( "Possibilities: [modi]fy, [modr]dn\n" );
538 exit( EXIT_SUCCESS );
541 case 'r': /* result or remove */
542 switch ( command3 ) {
543 case 's': /* result */
544 getline( line, sizeof(line), stdin,
545 "msgid (-1=>any)? " );
546 if ( line[0] == '\0' )
550 getline( line, sizeof(line), stdin,
551 "all (0=>any, 1=>all)? " );
552 if ( line[0] == '\0' )
556 if (( msgtype = ldap_result( ld, id, all,
557 &timeout, &res )) < 1 ) {
558 ldap_perror( ld, "ldap_result" );
561 printf( "\nresult: msgtype %d msgid %d\n",
562 msgtype, res->lm_msgid );
563 handle_result( ld, res );
567 case 'm': /* remove */
568 getline( dn, sizeof(dn), stdin, "dn? " );
569 strcat( dn, dnsuffix );
570 if ( (id = ldap_delete( ld, dn )) == -1 )
571 ldap_perror( ld, "ldap_delete" );
573 printf( "Remove initiated with id %d\n",
578 printf( "Possibilities: [rem]ove, [res]ult\n" );
583 case 's': /* search */
584 getline( dn, sizeof(dn), stdin, "searchbase? " );
585 strcat( dn, dnsuffix );
586 getline( line, sizeof(line), stdin,
587 "scope (0=Base, 1=One Level, 2=Subtree)? " );
588 scope = atoi( line );
589 getline( filter, sizeof(filter), stdin,
590 "search filter (e.g. sn=jones)? " );
591 types = get_list( "attrs to return? " );
592 getline( line, sizeof(line), stdin,
593 "attrsonly (0=attrs&values, 1=attrs only)? " );
594 attrsonly = atoi( line );
596 if (( id = ldap_search( ld, dn, scope, filter,
597 types, attrsonly )) == -1 ) {
598 ldap_perror( ld, "ldap_search" );
600 printf( "Search initiated with id %d\n", id );
605 case 't': /* set timeout value */
606 getline( line, sizeof(line), stdin, "timeout? " );
607 timeout.tv_sec = atoi( line );
610 case 'p': /* parse LDAP URL */
611 getline( line, sizeof(line), stdin, "LDAP URL? " );
612 if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
613 fprintf( stderr, "ldap_url_parse: error %d\n", i );
615 printf( "\t host: " );
616 if ( ludp->lud_host == NULL ) {
617 printf( "DEFAULT\n" );
619 printf( "<%s>\n", ludp->lud_host );
621 printf( "\t port: " );
622 if ( ludp->lud_port == 0 ) {
623 printf( "DEFAULT\n" );
625 printf( "%d\n", ludp->lud_port );
627 printf( "\t dn: <%s>\n", ludp->lud_dn );
628 printf( "\t attrs:" );
629 if ( ludp->lud_attrs == NULL ) {
632 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
633 printf( " <%s>", ludp->lud_attrs[ i ] );
636 printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
637 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
638 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
639 printf( "\tfilter: <%s>\n", ludp->lud_filter );
640 ldap_free_urldesc( ludp );
644 case 'n': /* set dn suffix, for convenience */
645 getline( line, sizeof(line), stdin, "DN suffix? " );
646 strcpy( dnsuffix, line );
649 case 'e': /* enable cache */
651 printf( NOCACHEERRMSG );
652 #else /* LDAP_NOCACHE */
653 getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
655 getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
656 if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
657 printf( "local cache is on\n" );
659 printf( "ldap_enable_cache failed\n" );
661 #endif /* LDAP_NOCACHE */
664 case 'x': /* uncache entry */
666 printf( NOCACHEERRMSG );
667 #else /* LDAP_NOCACHE */
668 getline( line, sizeof(line), stdin, "DN? " );
669 ldap_uncache_entry( ld, line );
670 #endif /* LDAP_NOCACHE */
673 case 'X': /* uncache request */
675 printf( NOCACHEERRMSG );
676 #else /* LDAP_NOCACHE */
677 getline( line, sizeof(line), stdin, "request msgid? " );
678 ldap_uncache_request( ld, atoi( line ));
679 #endif /* LDAP_NOCACHE */
682 case 'o': /* set ldap options */
683 getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
684 ld->ld_deref = atoi( line );
685 getline( line, sizeof(line), stdin, "timelimit?" );
686 ld->ld_timelimit = atoi( line );
687 getline( line, sizeof(line), stdin, "sizelimit?" );
688 ld->ld_sizelimit = atoi( line );
690 LDAP_BOOL_ZERO(&ld->ld_options);
692 getline( line, sizeof(line), stdin,
693 "Recognize and chase referrals (0=no, 1=yes)?" );
694 if ( atoi( line ) != 0 ) {
695 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
696 getline( line, sizeof(line), stdin,
697 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
698 if ( atoi( line ) != 0 ) {
699 ldap_set_rebind_proc( ld, bind_prompt, NULL );
704 case 'O': /* set cache options */
706 printf( NOCACHEERRMSG );
707 #else /* LDAP_NOCACHE */
708 getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
709 switch( atoi( line )) {
711 ldap_set_cache_options( ld, 0 );
714 ldap_set_cache_options( ld,
715 LDAP_CACHE_OPT_CACHENOERRS );
718 ldap_set_cache_options( ld,
719 LDAP_CACHE_OPT_CACHEALLERRS );
722 printf( "not a valid cache option\n" );
724 #endif /* LDAP_NOCACHE */
728 printf( "Commands: [ad]d [ab]andon [b]ind\n" );
729 printf( " [B]ind async [c]ompare\n" );
730 printf( " [modi]fy [modr]dn [rem]ove\n" );
731 printf( " [res]ult [s]earch [q]uit/unbind\n\n" );
732 printf( " [d]ebug [e]nable cache set ms[g]id\n" );
733 printf( " d[n]suffix [t]imeout [v]ersion\n" );
734 printf( " [?]help [o]ptions [O]cache options\n" );
735 printf( " [E]xplode dn [p]arse LDAP URL\n" );
736 printf( " [x]uncache entry [X]uncache request\n" );
740 printf( "Invalid command. Type ? for help.\n" );
744 (void) memset( line, '\0', sizeof(line) );
751 handle_result( LDAP *ld, LDAPMessage *lm )
753 switch ( lm->lm_msgtype ) {
754 case LDAP_RES_COMPARE:
755 printf( "Compare result\n" );
756 print_ldap_result( ld, lm, "compare" );
759 case LDAP_RES_SEARCH_RESULT:
760 printf( "Search result\n" );
761 print_ldap_result( ld, lm, "search" );
764 case LDAP_RES_SEARCH_ENTRY:
765 printf( "Search entry\n" );
766 print_search_entry( ld, lm );
770 printf( "Add result\n" );
771 print_ldap_result( ld, lm, "add" );
774 case LDAP_RES_DELETE:
775 printf( "Delete result\n" );
776 print_ldap_result( ld, lm, "delete" );
779 case LDAP_RES_MODRDN:
780 printf( "ModRDN result\n" );
781 print_ldap_result( ld, lm, "modrdn" );
785 printf( "Bind result\n" );
786 print_ldap_result( ld, lm, "bind" );
790 printf( "Unknown result type 0x%lx\n",
791 (unsigned long) lm->lm_msgtype );
792 print_ldap_result( ld, lm, "unknown" );
797 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
799 ldap_result2error( ld, lm, 1 );
800 ldap_perror( ld, s );
802 if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
803 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
804 if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
805 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
810 print_search_entry( LDAP *ld, LDAPMessage *res )
814 for ( e = ldap_first_entry( ld, res ); e != NULL;
815 e = ldap_next_entry( ld, e ) )
817 BerElement *ber = NULL;
820 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
823 dn = ldap_get_dn( ld, e );
824 printf( "\tDN: %s\n", dn );
826 ufn = ldap_dn2ufn( dn );
827 printf( "\tUFN: %s\n", ufn );
832 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
833 a = ldap_next_attribute( ld, e, ber ) )
835 struct berval **vals;
837 printf( "\t\tATTR: %s\n", a );
838 if ( (vals = ldap_get_values_len( ld, e, a ))
840 printf( "\t\t\t(no values)\n" );
843 for ( i = 0; vals[i] != NULL; i++ ) {
847 for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
848 if ( !isascii( vals[i]->bv_val[j] ) ) {
854 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
855 #ifdef BPRINT_NONASCII
856 ber_bprint( vals[i]->bv_val,
858 #endif /* BPRINT_NONASCII */
861 printf( "\t\t\tlength (%ld) %s\n",
862 vals[i]->bv_len, vals[i]->bv_val );
864 ber_bvecfree( vals );
873 if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
874 || res->lm_chain != NULL )
875 print_ldap_result( ld, res, "search" );