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
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 static char *dnsuffix;
54 getline( char *line, int len, FILE *fp, const char *prompt )
58 if ( fgets( line, len, fp ) == NULL )
61 line[ strlen( line ) - 1 ] = '\0';
68 get_list( const 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( const 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 );
163 const char *prompt3 )
165 static char buf[256];
169 struct berval **bvals;
175 getline( buf, sizeof(buf), stdin, prompt1 );
176 tmp.mod_op = atoi( buf );
178 if ( tmp.mod_op == -1 || buf[0] == '\0' )
182 getline( buf, sizeof(buf), stdin, prompt2 );
183 if ( buf[0] == '\0' )
185 tmp.mod_type = strdup( buf );
187 tmp.mod_values = get_list( prompt3 );
189 if ( tmp.mod_values != NULL ) {
192 for ( i = 0; tmp.mod_values[i] != NULL; ++i )
194 bvals = (struct berval **)calloc( i + 1,
195 sizeof( struct berval *));
196 for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
197 bvals[i] = (struct berval *)malloc(
198 sizeof( struct berval ));
199 if ( strncmp( tmp.mod_values[i], "{FILE}",
201 if ( file_read( tmp.mod_values[i] + 6,
206 bvals[i]->bv_val = tmp.mod_values[i];
208 strlen( tmp.mod_values[i] );
211 tmp.mod_bvalues = bvals;
212 tmp.mod_op |= LDAP_MOD_BVALUES;
215 if ( result == NULL )
216 result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
218 result = (LDAPMod **) realloc( result,
219 sizeof(LDAPMod *) * (num + 1) );
221 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
222 *(result[num]) = tmp; /* struct copy */
225 if ( result == NULL )
227 result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
235 bind_prompt( LDAP *ld,
236 LDAP_CONST char *url,
237 ber_tag_t request, ber_int_t msgid,
240 static char dn[256], passwd[256];
243 printf("rebind for request=%ld msgid=%ld url=%s\n",
244 request, (long) msgid, url );
246 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
247 getline( dn, sizeof(dn), stdin,
248 "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
249 if (( authmethod = atoi( dn )) == 3 ) {
250 authmethod = LDAP_AUTH_KRBV4;
254 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
255 authmethod = LDAP_AUTH_SIMPLE;
256 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
258 getline( dn, sizeof(dn), stdin, "re-bind dn? " );
259 strcat( dn, dnsuffix );
261 if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
262 getline( passwd, sizeof(passwd), stdin,
263 "re-bind password? " );
268 return ldap_bind_s( ld, dn, passwd, authmethod);
273 main( int argc, char **argv )
276 int i, c, port, errflg, method, id, msgtype;
277 char line[256], command1, command2, command3;
278 char passwd[64], dn[256], rdn[64], attr[64], value[256];
279 char filter[256], *host, **types;
281 char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
282 int bound, all, scope, attrsonly;
284 LDAPMod **mods, **attrs;
285 struct timeval timeout;
286 char *copyfname = NULL;
295 while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
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( "ldap_init( %s, %d )\n",
348 host == NULL ? "(null)" : host, port );
350 ld = ldap_init( host, port );
353 perror( "ldap_init" );
354 exit( EXIT_FAILURE );
357 if ( copyfname != NULL ) {
358 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
361 exit ( EXIT_FAILURE );
363 ld->ld_sb->sb_options = copyoptions;
370 (void) memset( line, '\0', sizeof(line) );
371 while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
376 switch ( command1 ) {
377 case 'a': /* add or abandon */
378 switch ( command2 ) {
380 getline( dn, sizeof(dn), stdin, "dn? " );
381 strcat( dn, dnsuffix );
382 if ( (attrs = get_modlist( NULL, "attr? ",
383 "value? " )) == NULL )
385 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
386 ldap_perror( ld, "ldap_add" );
388 printf( "Add initiated with id %d\n",
392 case 'b': /* abandon */
393 getline( line, sizeof(line), stdin, "msgid? " );
395 if ( ldap_abandon( ld, id ) != 0 )
396 ldap_perror( ld, "ldap_abandon" );
398 printf( "Abandon successful\n" );
401 printf( "Possibilities: [ad]d, [ab]ort\n" );
405 case 'b': /* asynch bind */
406 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
407 getline( line, sizeof(line), stdin,
408 "method (0->simple, 1->krbv41, 2->krbv42)? " );
409 method = atoi( line ) | 0x80;
410 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
411 method = LDAP_AUTH_SIMPLE;
412 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
413 getline( dn, sizeof(dn), stdin, "dn? " );
414 strcat( dn, dnsuffix );
416 if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
417 getline( passwd, sizeof(passwd), stdin,
422 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
423 fprintf( stderr, "ldap_bind failed\n" );
424 ldap_perror( ld, "ldap_bind" );
426 printf( "Bind initiated\n" );
431 case 'B': /* synch bind */
432 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
433 getline( line, sizeof(line), stdin,
434 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
435 method = atoi( line );
437 method = LDAP_AUTH_KRBV4;
439 method = method | 0x80;
440 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
441 method = LDAP_AUTH_SIMPLE;
442 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
443 getline( dn, sizeof(dn), stdin, "dn? " );
444 strcat( dn, dnsuffix );
447 getline( passwd, sizeof(passwd), stdin,
452 if ( ldap_bind_s( ld, dn, passwd, method ) !=
454 fprintf( stderr, "ldap_bind_s failed\n" );
455 ldap_perror( ld, "ldap_bind_s" );
457 printf( "Bind successful\n" );
462 case 'c': /* compare */
463 getline( dn, sizeof(dn), stdin, "dn? " );
464 strcat( dn, dnsuffix );
465 getline( attr, sizeof(attr), stdin, "attr? " );
466 getline( value, sizeof(value), stdin, "value? " );
468 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
469 ldap_perror( ld, "ldap_compare" );
471 printf( "Compare initiated with id %d\n", id );
474 case 'd': /* turn on debugging */
476 getline( line, sizeof(line), stdin, "debug level? " );
477 ldap_debug = atoi( line );
479 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
480 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
484 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
488 case 'E': /* explode a dn */
489 getline( line, sizeof(line), stdin, "dn? " );
490 exdn = ldap_explode_dn( line, 0 );
491 for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
492 printf( "\t%s\n", exdn[i] );
496 case 'g': /* set next msgid */
497 getline( line, sizeof(line), stdin, "msgid? " );
498 ld->ld_msgid = atoi( line );
501 case 'v': /* set version number */
502 getline( line, sizeof(line), stdin, "version? " );
503 ld->ld_version = atoi( line );
506 case 'm': /* modify or modifyrdn */
507 if ( strncmp( line, "modify", 4 ) == 0 ) {
508 getline( dn, sizeof(dn), stdin, "dn? " );
509 strcat( dn, dnsuffix );
510 if ( (mods = get_modlist(
511 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
512 "attribute type? ", "attribute value? " ))
515 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
516 ldap_perror( ld, "ldap_modify" );
518 printf( "Modify initiated with id %d\n",
520 } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
521 getline( dn, sizeof(dn), stdin, "dn? " );
522 strcat( dn, dnsuffix );
523 getline( rdn, sizeof(rdn), stdin, "newrdn? " );
524 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
525 ldap_perror( ld, "ldap_modrdn" );
527 printf( "Modrdn initiated with id %d\n",
530 printf( "Possibilities: [modi]fy, [modr]dn\n" );
536 exit( EXIT_SUCCESS );
539 case 'r': /* result or remove */
540 switch ( command3 ) {
541 case 's': /* result */
542 getline( line, sizeof(line), stdin,
543 "msgid (-1=>any)? " );
544 if ( line[0] == '\0' )
548 getline( line, sizeof(line), stdin,
549 "all (0=>any, 1=>all)? " );
550 if ( line[0] == '\0' )
554 if (( msgtype = ldap_result( ld, id, all,
555 &timeout, &res )) < 1 ) {
556 ldap_perror( ld, "ldap_result" );
559 printf( "\nresult: msgtype %d msgid %d\n",
560 msgtype, res->lm_msgid );
561 handle_result( ld, res );
565 case 'm': /* remove */
566 getline( dn, sizeof(dn), stdin, "dn? " );
567 strcat( dn, dnsuffix );
568 if ( (id = ldap_delete( ld, dn )) == -1 )
569 ldap_perror( ld, "ldap_delete" );
571 printf( "Remove initiated with id %d\n",
576 printf( "Possibilities: [rem]ove, [res]ult\n" );
581 case 's': /* search */
582 getline( dn, sizeof(dn), stdin, "searchbase? " );
583 strcat( dn, dnsuffix );
584 getline( line, sizeof(line), stdin,
585 "scope (0=Base, 1=One Level, 2=Subtree)? " );
586 scope = atoi( line );
587 getline( filter, sizeof(filter), stdin,
588 "search filter (e.g. sn=jones)? " );
589 types = get_list( "attrs to return? " );
590 getline( line, sizeof(line), stdin,
591 "attrsonly (0=attrs&values, 1=attrs only)? " );
592 attrsonly = atoi( line );
594 if (( id = ldap_search( ld, dn, scope, filter,
595 types, attrsonly )) == -1 ) {
596 ldap_perror( ld, "ldap_search" );
598 printf( "Search initiated with id %d\n", id );
603 case 't': /* set timeout value */
604 getline( line, sizeof(line), stdin, "timeout? " );
605 timeout.tv_sec = atoi( line );
608 case 'p': /* parse LDAP URL */
609 getline( line, sizeof(line), stdin, "LDAP URL? " );
610 if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
611 fprintf( stderr, "ldap_url_parse: error %d\n", i );
613 printf( "\t host: " );
614 if ( ludp->lud_host == NULL ) {
615 printf( "DEFAULT\n" );
617 printf( "<%s>\n", ludp->lud_host );
619 printf( "\t port: " );
620 if ( ludp->lud_port == 0 ) {
621 printf( "DEFAULT\n" );
623 printf( "%d\n", ludp->lud_port );
625 printf( "\t dn: <%s>\n", ludp->lud_dn );
626 printf( "\t attrs:" );
627 if ( ludp->lud_attrs == NULL ) {
630 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
631 printf( " <%s>", ludp->lud_attrs[ i ] );
634 printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
635 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
636 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
637 printf( "\tfilter: <%s>\n", ludp->lud_filter );
638 ldap_free_urldesc( ludp );
642 case 'n': /* set dn suffix, for convenience */
643 getline( line, sizeof(line), stdin, "DN suffix? " );
644 strcpy( dnsuffix, line );
647 case 'o': /* set ldap options */
648 getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
649 ld->ld_deref = atoi( line );
650 getline( line, sizeof(line), stdin, "timelimit?" );
651 ld->ld_timelimit = atoi( line );
652 getline( line, sizeof(line), stdin, "sizelimit?" );
653 ld->ld_sizelimit = atoi( line );
655 LDAP_BOOL_ZERO(&ld->ld_options);
657 getline( line, sizeof(line), stdin,
658 "Recognize and chase referrals (0=no, 1=yes)?" );
659 if ( atoi( line ) != 0 ) {
660 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
661 getline( line, sizeof(line), stdin,
662 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
663 if ( atoi( line ) != 0 ) {
664 ldap_set_rebind_proc( ld, bind_prompt, NULL );
671 "Commands: [ad]d [ab]andon [b]ind\n"
672 " [B]ind async [c]ompare\n"
673 " [modi]fy [modr]dn [rem]ove\n"
674 " [res]ult [s]earch [q]uit/unbind\n\n"
675 " [d]ebug set ms[g]id\n"
676 " d[n]suffix [t]imeout [v]ersion\n"
678 " [E]xplode dn [p]arse LDAP URL\n" );
682 printf( "Invalid command. Type ? for help.\n" );
686 (void) memset( line, '\0', sizeof(line) );
693 handle_result( LDAP *ld, LDAPMessage *lm )
695 switch ( lm->lm_msgtype ) {
696 case LDAP_RES_COMPARE:
697 printf( "Compare result\n" );
698 print_ldap_result( ld, lm, "compare" );
701 case LDAP_RES_SEARCH_RESULT:
702 printf( "Search result\n" );
703 print_ldap_result( ld, lm, "search" );
706 case LDAP_RES_SEARCH_ENTRY:
707 printf( "Search entry\n" );
708 print_search_entry( ld, lm );
712 printf( "Add result\n" );
713 print_ldap_result( ld, lm, "add" );
716 case LDAP_RES_DELETE:
717 printf( "Delete result\n" );
718 print_ldap_result( ld, lm, "delete" );
721 case LDAP_RES_MODRDN:
722 printf( "ModRDN result\n" );
723 print_ldap_result( ld, lm, "modrdn" );
727 printf( "Bind result\n" );
728 print_ldap_result( ld, lm, "bind" );
732 printf( "Unknown result type 0x%lx\n",
733 (unsigned long) lm->lm_msgtype );
734 print_ldap_result( ld, lm, "unknown" );
739 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
741 ldap_result2error( ld, lm, 1 );
742 ldap_perror( ld, s );
744 if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
745 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
746 if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
747 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
752 print_search_entry( LDAP *ld, LDAPMessage *res )
756 for ( e = ldap_first_entry( ld, res ); e != NULL;
757 e = ldap_next_entry( ld, e ) )
759 BerElement *ber = NULL;
762 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
765 dn = ldap_get_dn( ld, e );
766 printf( "\tDN: %s\n", dn );
768 ufn = ldap_dn2ufn( dn );
769 printf( "\tUFN: %s\n", ufn );
774 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
775 a = ldap_next_attribute( ld, e, ber ) )
777 struct berval **vals;
779 printf( "\t\tATTR: %s\n", a );
780 if ( (vals = ldap_get_values_len( ld, e, a ))
782 printf( "\t\t\t(no values)\n" );
785 for ( i = 0; vals[i] != NULL; i++ ) {
789 for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
790 if ( !isascii( vals[i]->bv_val[j] ) ) {
796 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
797 #ifdef BPRINT_NONASCII
798 ber_bprint( vals[i]->bv_val,
800 #endif /* BPRINT_NONASCII */
803 printf( "\t\t\tlength (%ld) %s\n",
804 vals[i]->bv_len, vals[i]->bv_val );
806 ber_bvecfree( vals );
815 if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
816 || res->lm_chain != NULL )
817 print_ldap_result( ld, res, "search" );