3 * Copyright 1998-2003 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
37 static char *get_line LDAP_P(( char *line, int len, FILE *fp, const char *prompt ));
38 static char **get_list LDAP_P(( const char *prompt ));
39 static int file_read LDAP_P(( const char *path, struct berval *bv ));
40 static LDAPMod **get_modlist LDAP_P(( const char *prompt1,
41 const char *prompt2, const char *prompt3 ));
42 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
43 static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm,
45 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
46 static void free_list LDAP_P(( char **list ));
48 static char *dnsuffix;
51 get_line( char *line, int len, FILE *fp, const char *prompt )
55 if ( fgets( line, len, fp ) == NULL )
58 line[ strlen( line ) - 1 ] = '\0';
65 get_list( const char *prompt )
74 get_line( buf, sizeof(buf), stdin, prompt );
79 if ( result == (char **) 0 )
80 result = (char **) malloc( sizeof(char *) );
82 result = (char **) realloc( result,
83 sizeof(char *) * (num + 1) );
85 result[num++] = (char *) strdup( buf );
87 if ( result == (char **) 0 )
89 result = (char **) realloc( result, sizeof(char *) * (num + 1) );
97 free_list( char **list )
101 if ( list != NULL ) {
102 for ( i = 0; list[ i ] != NULL; ++i ) {
105 free( (char *)list );
111 file_read( const char *path, struct berval *bv )
117 if (( fp = fopen( path, "r" )) == NULL ) {
122 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
128 bv->bv_len = ftell( fp );
130 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
136 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
142 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
146 if ( (ber_len_t) rlen != bv->bv_len ) {
152 return( bv->bv_len );
160 const char *prompt3 )
162 static char buf[256];
166 struct berval **bvals;
172 get_line( buf, sizeof(buf), stdin, prompt1 );
173 tmp.mod_op = atoi( buf );
175 if ( tmp.mod_op == -1 || buf[0] == '\0' )
179 get_line( 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,
233 LDAP_CONST char *url,
234 ber_tag_t request, ber_int_t msgid,
237 static char dn[256], passwd[256];
240 printf("rebind for request=%ld msgid=%ld url=%s\n",
241 request, (long) msgid, url );
243 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
244 get_line( dn, sizeof(dn), stdin,
245 "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
246 if (( authmethod = atoi( dn )) == 3 ) {
247 authmethod = LDAP_AUTH_KRBV4;
251 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
252 authmethod = LDAP_AUTH_SIMPLE;
253 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
255 get_line( dn, sizeof(dn), stdin, "re-bind dn? " );
256 strcat( dn, dnsuffix );
258 if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
259 get_line( passwd, sizeof(passwd), stdin,
260 "re-bind password? " );
265 return ldap_bind_s( ld, dn, passwd, authmethod);
270 main( int argc, char **argv )
273 int i, c, port, errflg, method, id, msgtype;
274 char line[256], command1, command2, command3;
275 char passwd[64], dn[256], rdn[64], attr[64], value[256];
276 char filter[256], *host, **types;
278 char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
279 int bound, all, scope, attrsonly;
281 LDAPMod **mods, **attrs;
282 struct timeval timeout;
283 char *copyfname = NULL;
292 while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
296 ldap_debug = atoi( optarg );
298 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
299 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
303 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
316 port = atoi( optarg );
319 case 't': /* copy ber's to given file */
320 copyfname = strdup( optarg );
321 /* copyoptions = LBER_TO_FILE; */
324 case 'T': /* only output ber's to given file */
325 copyfname = strdup( optarg );
326 /* copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
334 if ( host == NULL && optind == argc - 1 ) {
335 host = argv[ optind ];
339 if ( errflg || optind < argc - 1 ) {
340 fprintf( stderr, usage, argv[ 0 ] );
341 exit( EXIT_FAILURE );
344 printf( "ldap_init( %s, %d )\n",
345 host == NULL ? "(null)" : host, port );
347 ld = ldap_init( host, port );
350 perror( "ldap_init" );
351 exit( EXIT_FAILURE );
354 if ( copyfname != NULL ) {
355 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
358 exit ( EXIT_FAILURE );
360 ld->ld_sb->sb_options = copyoptions;
367 (void) memset( line, '\0', sizeof(line) );
368 while ( get_line( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
373 switch ( command1 ) {
374 case 'a': /* add or abandon */
375 switch ( command2 ) {
377 get_line( dn, sizeof(dn), stdin, "dn? " );
378 strcat( dn, dnsuffix );
379 if ( (attrs = get_modlist( NULL, "attr? ",
380 "value? " )) == NULL )
382 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
383 ldap_perror( ld, "ldap_add" );
385 printf( "Add initiated with id %d\n",
389 case 'b': /* abandon */
390 get_line( line, sizeof(line), stdin, "msgid? " );
392 if ( ldap_abandon( ld, id ) != 0 )
393 ldap_perror( ld, "ldap_abandon" );
395 printf( "Abandon successful\n" );
398 printf( "Possibilities: [ad]d, [ab]ort\n" );
402 case 'b': /* asynch bind */
403 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
404 get_line( line, sizeof(line), stdin,
405 "method (0->simple, 1->krbv41, 2->krbv42)? " );
406 method = atoi( line ) | 0x80;
407 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
408 method = LDAP_AUTH_SIMPLE;
409 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
410 get_line( dn, sizeof(dn), stdin, "dn? " );
411 strcat( dn, dnsuffix );
413 if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
414 get_line( passwd, sizeof(passwd), stdin,
419 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
420 fprintf( stderr, "ldap_bind failed\n" );
421 ldap_perror( ld, "ldap_bind" );
423 printf( "Bind initiated\n" );
428 case 'B': /* synch bind */
429 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
430 get_line( line, sizeof(line), stdin,
431 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
432 method = atoi( line );
434 method = LDAP_AUTH_KRBV4;
436 method = method | 0x80;
437 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
438 method = LDAP_AUTH_SIMPLE;
439 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
440 get_line( dn, sizeof(dn), stdin, "dn? " );
441 strcat( dn, dnsuffix );
444 get_line( passwd, sizeof(passwd), stdin,
449 if ( ldap_bind_s( ld, dn, passwd, method ) !=
451 fprintf( stderr, "ldap_bind_s failed\n" );
452 ldap_perror( ld, "ldap_bind_s" );
454 printf( "Bind successful\n" );
459 case 'c': /* compare */
460 get_line( dn, sizeof(dn), stdin, "dn? " );
461 strcat( dn, dnsuffix );
462 get_line( attr, sizeof(attr), stdin, "attr? " );
463 get_line( value, sizeof(value), stdin, "value? " );
465 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
466 ldap_perror( ld, "ldap_compare" );
468 printf( "Compare initiated with id %d\n", id );
471 case 'd': /* turn on debugging */
473 get_line( line, sizeof(line), stdin, "debug level? " );
474 ldap_debug = atoi( line );
476 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
477 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
481 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
485 case 'E': /* explode a dn */
486 get_line( line, sizeof(line), stdin, "dn? " );
487 exdn = ldap_explode_dn( line, 0 );
488 for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
489 printf( "\t%s\n", exdn[i] );
493 case 'g': /* set next msgid */
494 get_line( line, sizeof(line), stdin, "msgid? " );
495 ld->ld_msgid = atoi( line );
498 case 'v': /* set version number */
499 get_line( line, sizeof(line), stdin, "version? " );
500 ld->ld_version = atoi( line );
503 case 'm': /* modify or modifyrdn */
504 if ( strncmp( line, "modify", 4 ) == 0 ) {
505 get_line( dn, sizeof(dn), stdin, "dn? " );
506 strcat( dn, dnsuffix );
507 if ( (mods = get_modlist(
508 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
509 "attribute type? ", "attribute value? " ))
512 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
513 ldap_perror( ld, "ldap_modify" );
515 printf( "Modify initiated with id %d\n",
517 } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
518 get_line( dn, sizeof(dn), stdin, "dn? " );
519 strcat( dn, dnsuffix );
520 get_line( rdn, sizeof(rdn), stdin, "newrdn? " );
521 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
522 ldap_perror( ld, "ldap_modrdn" );
524 printf( "Modrdn initiated with id %d\n",
527 printf( "Possibilities: [modi]fy, [modr]dn\n" );
533 exit( EXIT_SUCCESS );
536 case 'r': /* result or remove */
537 switch ( command3 ) {
538 case 's': /* result */
539 get_line( line, sizeof(line), stdin,
540 "msgid (-1=>any)? " );
541 if ( line[0] == '\0' )
545 get_line( line, sizeof(line), stdin,
546 "all (0=>any, 1=>all)? " );
547 if ( line[0] == '\0' )
551 if (( msgtype = ldap_result( ld, id, all,
552 &timeout, &res )) < 1 ) {
553 ldap_perror( ld, "ldap_result" );
556 printf( "\nresult: msgtype %d msgid %d\n",
557 msgtype, res->lm_msgid );
558 handle_result( ld, res );
562 case 'm': /* remove */
563 get_line( dn, sizeof(dn), stdin, "dn? " );
564 strcat( dn, dnsuffix );
565 if ( (id = ldap_delete( ld, dn )) == -1 )
566 ldap_perror( ld, "ldap_delete" );
568 printf( "Remove initiated with id %d\n",
573 printf( "Possibilities: [rem]ove, [res]ult\n" );
578 case 's': /* search */
579 get_line( dn, sizeof(dn), stdin, "searchbase? " );
580 strcat( dn, dnsuffix );
581 get_line( line, sizeof(line), stdin,
582 "scope (0=Base, 1=One Level, 2=Subtree)? " );
583 scope = atoi( line );
584 get_line( filter, sizeof(filter), stdin,
585 "search filter (e.g. sn=jones)? " );
586 types = get_list( "attrs to return? " );
587 get_line( line, sizeof(line), stdin,
588 "attrsonly (0=attrs&values, 1=attrs only)? " );
589 attrsonly = atoi( line );
591 if (( id = ldap_search( ld, dn, scope, filter,
592 types, attrsonly )) == -1 ) {
593 ldap_perror( ld, "ldap_search" );
595 printf( "Search initiated with id %d\n", id );
600 case 't': /* set timeout value */
601 get_line( line, sizeof(line), stdin, "timeout? " );
602 timeout.tv_sec = atoi( line );
605 case 'p': /* parse LDAP URL */
606 get_line( line, sizeof(line), stdin, "LDAP URL? " );
607 if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
608 fprintf( stderr, "ldap_url_parse: error %d\n", i );
610 printf( "\t host: " );
611 if ( ludp->lud_host == NULL ) {
612 printf( "DEFAULT\n" );
614 printf( "<%s>\n", ludp->lud_host );
616 printf( "\t port: " );
617 if ( ludp->lud_port == 0 ) {
618 printf( "DEFAULT\n" );
620 printf( "%d\n", ludp->lud_port );
622 printf( "\t dn: <%s>\n", ludp->lud_dn );
623 printf( "\t attrs:" );
624 if ( ludp->lud_attrs == NULL ) {
627 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
628 printf( " <%s>", ludp->lud_attrs[ i ] );
631 printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
632 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
633 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
634 printf( "\tfilter: <%s>\n", ludp->lud_filter );
635 ldap_free_urldesc( ludp );
639 case 'n': /* set dn suffix, for convenience */
640 get_line( line, sizeof(line), stdin, "DN suffix? " );
641 strcpy( dnsuffix, line );
644 case 'o': /* set ldap options */
645 get_line( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
646 ld->ld_deref = atoi( line );
647 get_line( line, sizeof(line), stdin, "timelimit?" );
648 ld->ld_timelimit = atoi( line );
649 get_line( line, sizeof(line), stdin, "sizelimit?" );
650 ld->ld_sizelimit = atoi( line );
652 LDAP_BOOL_ZERO(&ld->ld_options);
654 get_line( line, sizeof(line), stdin,
655 "Recognize and chase referrals (0=no, 1=yes)?" );
656 if ( atoi( line ) != 0 ) {
657 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
658 get_line( line, sizeof(line), stdin,
659 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
660 if ( atoi( line ) != 0 ) {
661 ldap_set_rebind_proc( ld, bind_prompt, NULL );
668 "Commands: [ad]d [ab]andon [b]ind\n"
669 " [B]ind async [c]ompare\n"
670 " [modi]fy [modr]dn [rem]ove\n"
671 " [res]ult [s]earch [q]uit/unbind\n\n"
672 " [d]ebug set ms[g]id\n"
673 " d[n]suffix [t]imeout [v]ersion\n"
675 " [E]xplode dn [p]arse LDAP URL\n" );
679 printf( "Invalid command. Type ? for help.\n" );
683 (void) memset( line, '\0', sizeof(line) );
690 handle_result( LDAP *ld, LDAPMessage *lm )
692 switch ( lm->lm_msgtype ) {
693 case LDAP_RES_COMPARE:
694 printf( "Compare result\n" );
695 print_ldap_result( ld, lm, "compare" );
698 case LDAP_RES_SEARCH_RESULT:
699 printf( "Search result\n" );
700 print_ldap_result( ld, lm, "search" );
703 case LDAP_RES_SEARCH_ENTRY:
704 printf( "Search entry\n" );
705 print_search_entry( ld, lm );
709 printf( "Add result\n" );
710 print_ldap_result( ld, lm, "add" );
713 case LDAP_RES_DELETE:
714 printf( "Delete result\n" );
715 print_ldap_result( ld, lm, "delete" );
718 case LDAP_RES_MODRDN:
719 printf( "ModRDN result\n" );
720 print_ldap_result( ld, lm, "modrdn" );
724 printf( "Bind result\n" );
725 print_ldap_result( ld, lm, "bind" );
729 printf( "Unknown result type 0x%lx\n",
730 (unsigned long) lm->lm_msgtype );
731 print_ldap_result( ld, lm, "unknown" );
736 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
738 ldap_result2error( ld, lm, 1 );
739 ldap_perror( ld, s );
741 if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
742 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
743 if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
744 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
749 print_search_entry( LDAP *ld, LDAPMessage *res )
753 for ( e = ldap_first_entry( ld, res ); e != NULL;
754 e = ldap_next_entry( ld, e ) )
756 BerElement *ber = NULL;
759 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
762 dn = ldap_get_dn( ld, e );
763 printf( "\tDN: %s\n", dn );
765 ufn = ldap_dn2ufn( dn );
766 printf( "\tUFN: %s\n", ufn );
771 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
772 a = ldap_next_attribute( ld, e, ber ) )
774 struct berval **vals;
776 printf( "\t\tATTR: %s\n", a );
777 if ( (vals = ldap_get_values_len( ld, e, a ))
779 printf( "\t\t\t(no values)\n" );
782 for ( i = 0; vals[i] != NULL; i++ ) {
786 for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
787 if ( !isascii( vals[i]->bv_val[j] ) ) {
793 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
794 #ifdef BPRINT_NONASCII
795 ber_bprint( vals[i]->bv_val,
797 #endif /* BPRINT_NONASCII */
800 printf( "\t\t\tlength (%ld) %s\n",
801 vals[i]->bv_len, vals[i]->bv_val );
803 ber_bvecfree( vals );
812 if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
813 || res->lm_chain != NULL )
814 print_ldap_result( ld, res, "search" );