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';
64 get_list( const char *prompt )
73 get_line( buf, sizeof(buf), stdin, prompt );
78 if ( result == (char **) 0 )
79 result = (char **) malloc( sizeof(char *) );
81 result = (char **) realloc( result,
82 sizeof(char *) * (num + 1) );
84 result[num++] = (char *) strdup( buf );
86 if ( result == (char **) 0 )
88 result = (char **) realloc( result, sizeof(char *) * (num + 1) );
96 free_list( char **list )
100 if ( list != NULL ) {
101 for ( i = 0; list[ i ] != NULL; ++i ) {
104 free( (char *)list );
110 file_read( const char *path, struct berval *bv )
116 if (( fp = fopen( path, "r" )) == NULL ) {
121 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
127 bv->bv_len = ftell( fp );
129 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
135 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
141 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
145 if ( (ber_len_t) rlen != bv->bv_len ) {
151 return( bv->bv_len );
159 const char *prompt3 )
161 static char buf[256];
165 struct berval **bvals;
171 get_line( buf, sizeof(buf), stdin, prompt1 );
172 tmp.mod_op = atoi( buf );
174 if ( tmp.mod_op == -1 || buf[0] == '\0' )
178 get_line( buf, sizeof(buf), stdin, prompt2 );
179 if ( buf[0] == '\0' )
181 tmp.mod_type = strdup( buf );
183 tmp.mod_values = get_list( prompt3 );
185 if ( tmp.mod_values != NULL ) {
188 for ( i = 0; tmp.mod_values[i] != NULL; ++i )
190 bvals = (struct berval **)calloc( i + 1,
191 sizeof( struct berval *));
192 for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
193 bvals[i] = (struct berval *)malloc(
194 sizeof( struct berval ));
195 if ( strncmp( tmp.mod_values[i], "{FILE}",
197 if ( file_read( tmp.mod_values[i] + 6,
202 bvals[i]->bv_val = tmp.mod_values[i];
204 strlen( tmp.mod_values[i] );
207 tmp.mod_bvalues = bvals;
208 tmp.mod_op |= LDAP_MOD_BVALUES;
211 if ( result == NULL )
212 result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
214 result = (LDAPMod **) realloc( result,
215 sizeof(LDAPMod *) * (num + 1) );
217 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
218 *(result[num]) = tmp; /* struct copy */
221 if ( result == NULL )
223 result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
231 bind_prompt( LDAP *ld,
232 LDAP_CONST char *url,
233 ber_tag_t request, ber_int_t msgid,
236 static char dn[256], passwd[256];
239 printf("rebind for request=%ld msgid=%ld url=%s\n",
240 request, (long) msgid, url );
242 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
243 get_line( dn, sizeof(dn), stdin,
244 "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
245 if (( authmethod = atoi( dn )) == 3 ) {
246 authmethod = LDAP_AUTH_KRBV4;
250 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
251 authmethod = LDAP_AUTH_SIMPLE;
252 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
254 get_line( dn, sizeof(dn), stdin, "re-bind dn? " );
255 strcat( dn, dnsuffix );
257 if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
258 get_line( passwd, sizeof(passwd), stdin,
259 "re-bind password? " );
264 return ldap_bind_s( ld, dn, passwd, authmethod);
269 main( int argc, char **argv )
272 int i, c, port, errflg, method, id, msgtype;
273 char line[256], command1, command2, command3;
274 char passwd[64], dn[256], rdn[64], attr[64], value[256];
275 char filter[256], *host, **types;
277 char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
278 int bound, all, scope, attrsonly;
280 LDAPMod **mods, **attrs;
281 struct timeval timeout;
282 char *copyfname = NULL;
291 while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
295 ldap_debug = atoi( optarg );
297 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
298 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
302 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
315 port = atoi( optarg );
318 case 't': /* copy ber's to given file */
319 copyfname = strdup( optarg );
320 /* copyoptions = LBER_TO_FILE; */
323 case 'T': /* only output ber's to given file */
324 copyfname = strdup( optarg );
325 /* copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
333 if ( host == NULL && optind == argc - 1 ) {
334 host = argv[ optind ];
338 if ( errflg || optind < argc - 1 ) {
339 fprintf( stderr, usage, argv[ 0 ] );
340 exit( EXIT_FAILURE );
343 printf( "ldap_init( %s, %d )\n",
344 host == NULL ? "(null)" : host, port );
346 ld = ldap_init( host, port );
349 perror( "ldap_init" );
350 exit( EXIT_FAILURE );
353 if ( copyfname != NULL ) {
354 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
357 exit ( EXIT_FAILURE );
359 ld->ld_sb->sb_options = copyoptions;
366 (void) memset( line, '\0', sizeof(line) );
367 while ( get_line( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
372 switch ( command1 ) {
373 case 'a': /* add or abandon */
374 switch ( command2 ) {
376 get_line( dn, sizeof(dn), stdin, "dn? " );
377 strcat( dn, dnsuffix );
378 if ( (attrs = get_modlist( NULL, "attr? ",
379 "value? " )) == NULL )
381 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
382 ldap_perror( ld, "ldap_add" );
384 printf( "Add initiated with id %d\n",
388 case 'b': /* abandon */
389 get_line( line, sizeof(line), stdin, "msgid? " );
391 if ( ldap_abandon( ld, id ) != 0 )
392 ldap_perror( ld, "ldap_abandon" );
394 printf( "Abandon successful\n" );
397 printf( "Possibilities: [ad]d, [ab]ort\n" );
401 case 'b': /* asynch bind */
402 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
403 get_line( line, sizeof(line), stdin,
404 "method (0->simple, 1->krbv41, 2->krbv42)? " );
405 method = atoi( line ) | 0x80;
406 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
407 method = LDAP_AUTH_SIMPLE;
408 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
409 get_line( dn, sizeof(dn), stdin, "dn? " );
410 strcat( dn, dnsuffix );
412 if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
413 get_line( passwd, sizeof(passwd), stdin,
418 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
419 fprintf( stderr, "ldap_bind failed\n" );
420 ldap_perror( ld, "ldap_bind" );
422 printf( "Bind initiated\n" );
427 case 'B': /* synch bind */
428 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
429 get_line( line, sizeof(line), stdin,
430 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
431 method = atoi( line );
433 method = LDAP_AUTH_KRBV4;
435 method = method | 0x80;
436 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
437 method = LDAP_AUTH_SIMPLE;
438 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
439 get_line( dn, sizeof(dn), stdin, "dn? " );
440 strcat( dn, dnsuffix );
443 get_line( passwd, sizeof(passwd), stdin,
448 if ( ldap_bind_s( ld, dn, passwd, method ) !=
450 fprintf( stderr, "ldap_bind_s failed\n" );
451 ldap_perror( ld, "ldap_bind_s" );
453 printf( "Bind successful\n" );
458 case 'c': /* compare */
459 get_line( dn, sizeof(dn), stdin, "dn? " );
460 strcat( dn, dnsuffix );
461 get_line( attr, sizeof(attr), stdin, "attr? " );
462 get_line( value, sizeof(value), stdin, "value? " );
464 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
465 ldap_perror( ld, "ldap_compare" );
467 printf( "Compare initiated with id %d\n", id );
470 case 'd': /* turn on debugging */
472 get_line( line, sizeof(line), stdin, "debug level? " );
473 ldap_debug = atoi( line );
475 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
476 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
480 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
484 case 'E': /* explode a dn */
485 get_line( line, sizeof(line), stdin, "dn? " );
486 exdn = ldap_explode_dn( line, 0 );
487 for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
488 printf( "\t%s\n", exdn[i] );
492 case 'g': /* set next msgid */
493 get_line( line, sizeof(line), stdin, "msgid? " );
494 ld->ld_msgid = atoi( line );
497 case 'v': /* set version number */
498 get_line( line, sizeof(line), stdin, "version? " );
499 ld->ld_version = atoi( line );
502 case 'm': /* modify or modifyrdn */
503 if ( strncmp( line, "modify", 4 ) == 0 ) {
504 get_line( dn, sizeof(dn), stdin, "dn? " );
505 strcat( dn, dnsuffix );
506 if ( (mods = get_modlist(
507 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
508 "attribute type? ", "attribute value? " ))
511 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
512 ldap_perror( ld, "ldap_modify" );
514 printf( "Modify initiated with id %d\n",
516 } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
517 get_line( dn, sizeof(dn), stdin, "dn? " );
518 strcat( dn, dnsuffix );
519 get_line( rdn, sizeof(rdn), stdin, "newrdn? " );
520 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
521 ldap_perror( ld, "ldap_modrdn" );
523 printf( "Modrdn initiated with id %d\n",
526 printf( "Possibilities: [modi]fy, [modr]dn\n" );
532 exit( EXIT_SUCCESS );
535 case 'r': /* result or remove */
536 switch ( command3 ) {
537 case 's': /* result */
538 get_line( line, sizeof(line), stdin,
539 "msgid (-1=>any)? " );
540 if ( line[0] == '\0' )
544 get_line( line, sizeof(line), stdin,
545 "all (0=>any, 1=>all)? " );
546 if ( line[0] == '\0' )
550 if (( msgtype = ldap_result( ld, id, all,
551 &timeout, &res )) < 1 ) {
552 ldap_perror( ld, "ldap_result" );
555 printf( "\nresult: msgtype %d msgid %d\n",
556 msgtype, res->lm_msgid );
557 handle_result( ld, res );
561 case 'm': /* remove */
562 get_line( dn, sizeof(dn), stdin, "dn? " );
563 strcat( dn, dnsuffix );
564 if ( (id = ldap_delete( ld, dn )) == -1 )
565 ldap_perror( ld, "ldap_delete" );
567 printf( "Remove initiated with id %d\n",
572 printf( "Possibilities: [rem]ove, [res]ult\n" );
577 case 's': /* search */
578 get_line( dn, sizeof(dn), stdin, "searchbase? " );
579 strcat( dn, dnsuffix );
580 get_line( line, sizeof(line), stdin,
581 "scope (0=Base, 1=One Level, 2=Subtree)? " );
582 scope = atoi( line );
583 get_line( filter, sizeof(filter), stdin,
584 "search filter (e.g. sn=jones)? " );
585 types = get_list( "attrs to return? " );
586 get_line( line, sizeof(line), stdin,
587 "attrsonly (0=attrs&values, 1=attrs only)? " );
588 attrsonly = atoi( line );
590 if (( id = ldap_search( ld, dn, scope, filter,
591 types, attrsonly )) == -1 ) {
592 ldap_perror( ld, "ldap_search" );
594 printf( "Search initiated with id %d\n", id );
599 case 't': /* set timeout value */
600 get_line( line, sizeof(line), stdin, "timeout? " );
601 timeout.tv_sec = atoi( line );
604 case 'p': /* parse LDAP URL */
605 get_line( line, sizeof(line), stdin, "LDAP URL? " );
606 if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
607 fprintf( stderr, "ldap_url_parse: error %d\n", i );
609 printf( "\t host: " );
610 if ( ludp->lud_host == NULL ) {
611 printf( "DEFAULT\n" );
613 printf( "<%s>\n", ludp->lud_host );
615 printf( "\t port: " );
616 if ( ludp->lud_port == 0 ) {
617 printf( "DEFAULT\n" );
619 printf( "%d\n", ludp->lud_port );
621 printf( "\t dn: <%s>\n", ludp->lud_dn );
622 printf( "\t attrs:" );
623 if ( ludp->lud_attrs == NULL ) {
626 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
627 printf( " <%s>", ludp->lud_attrs[ i ] );
630 printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
631 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
632 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
633 printf( "\tfilter: <%s>\n", ludp->lud_filter );
634 ldap_free_urldesc( ludp );
638 case 'n': /* set dn suffix, for convenience */
639 get_line( line, sizeof(line), stdin, "DN suffix? " );
640 strcpy( dnsuffix, line );
643 case 'o': /* set ldap options */
644 get_line( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
645 ld->ld_deref = atoi( line );
646 get_line( line, sizeof(line), stdin, "timelimit?" );
647 ld->ld_timelimit = atoi( line );
648 get_line( line, sizeof(line), stdin, "sizelimit?" );
649 ld->ld_sizelimit = atoi( line );
651 LDAP_BOOL_ZERO(&ld->ld_options);
653 get_line( line, sizeof(line), stdin,
654 "Recognize and chase referrals (0=no, 1=yes)?" );
655 if ( atoi( line ) != 0 ) {
656 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
657 get_line( line, sizeof(line), stdin,
658 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
659 if ( atoi( line ) != 0 ) {
660 ldap_set_rebind_proc( ld, bind_prompt, NULL );
667 "Commands: [ad]d [ab]andon [b]ind\n"
668 " [B]ind async [c]ompare\n"
669 " [modi]fy [modr]dn [rem]ove\n"
670 " [res]ult [s]earch [q]uit/unbind\n\n"
671 " [d]ebug set ms[g]id\n"
672 " d[n]suffix [t]imeout [v]ersion\n"
674 " [E]xplode dn [p]arse LDAP URL\n" );
678 printf( "Invalid command. Type ? for help.\n" );
682 (void) memset( line, '\0', sizeof(line) );
689 handle_result( LDAP *ld, LDAPMessage *lm )
691 switch ( lm->lm_msgtype ) {
692 case LDAP_RES_COMPARE:
693 printf( "Compare result\n" );
694 print_ldap_result( ld, lm, "compare" );
697 case LDAP_RES_SEARCH_RESULT:
698 printf( "Search result\n" );
699 print_ldap_result( ld, lm, "search" );
702 case LDAP_RES_SEARCH_ENTRY:
703 printf( "Search entry\n" );
704 print_search_entry( ld, lm );
708 printf( "Add result\n" );
709 print_ldap_result( ld, lm, "add" );
712 case LDAP_RES_DELETE:
713 printf( "Delete result\n" );
714 print_ldap_result( ld, lm, "delete" );
717 case LDAP_RES_MODRDN:
718 printf( "ModRDN result\n" );
719 print_ldap_result( ld, lm, "modrdn" );
723 printf( "Bind result\n" );
724 print_ldap_result( ld, lm, "bind" );
728 printf( "Unknown result type 0x%lx\n",
729 (unsigned long) lm->lm_msgtype );
730 print_ldap_result( ld, lm, "unknown" );
735 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
737 ldap_result2error( ld, lm, 1 );
738 ldap_perror( ld, s );
740 if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
741 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
742 if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
743 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
748 print_search_entry( LDAP *ld, LDAPMessage *res )
752 for ( e = ldap_first_entry( ld, res ); e != NULL;
753 e = ldap_next_entry( ld, e ) )
755 BerElement *ber = NULL;
758 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
761 dn = ldap_get_dn( ld, e );
762 printf( "\tDN: %s\n", dn );
764 ufn = ldap_dn2ufn( dn );
765 printf( "\tUFN: %s\n", ufn );
770 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
771 a = ldap_next_attribute( ld, e, ber ) )
773 struct berval **vals;
775 printf( "\t\tATTR: %s\n", a );
776 if ( (vals = ldap_get_values_len( ld, e, a ))
778 printf( "\t\t\t(no values)\n" );
781 for ( i = 0; vals[i] != NULL; i++ ) {
785 for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
786 if ( !isascii( vals[i]->bv_val[j] ) ) {
792 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
793 #ifdef BPRINT_NONASCII
794 ber_bprint( vals[i]->bv_val,
796 #endif /* BPRINT_NONASCII */
799 printf( "\t\t\tlength (%ld) %s\n",
800 vals[i]->bv_len, vals[i]->bv_val );
802 ber_bvecfree( vals );
811 if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
812 || res->lm_chain != NULL )
813 print_ldap_result( ld, res, "search" );