2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/stdlib.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
26 #include <ac/unistd.h>
30 #ifdef HAVE_SYS_FILE_H
39 /* including the "internal" defs is legit and nec. since this test routine has
40 * a-priori knowledge of libldap internal workings.
41 * hodges@stanford.edu 5-Feb-96
46 static char *get_line LDAP_P(( char *line, int len, FILE *fp, const char *prompt ));
47 static char **get_list LDAP_P(( const char *prompt ));
48 static int file_read LDAP_P(( const char *path, struct berval *bv ));
49 static LDAPMod **get_modlist LDAP_P(( const char *prompt1,
50 const char *prompt2, const char *prompt3 ));
51 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
52 static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm,
54 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
55 static void free_list LDAP_P(( char **list ));
57 static char *dnsuffix;
60 get_line( char *line, int len, FILE *fp, const char *prompt )
64 if ( fgets( line, len, fp ) == NULL )
67 line[ strlen( line ) - 1 ] = '\0';
73 get_list( const char *prompt )
82 get_line( buf, sizeof(buf), stdin, prompt );
87 if ( result == (char **) 0 )
88 result = (char **) malloc( sizeof(char *) );
90 result = (char **) realloc( result,
91 sizeof(char *) * (num + 1) );
93 result[num++] = (char *) strdup( buf );
95 if ( result == (char **) 0 )
97 result = (char **) realloc( result, sizeof(char *) * (num + 1) );
105 free_list( char **list )
109 if ( list != NULL ) {
110 for ( i = 0; list[ i ] != NULL; ++i ) {
113 free( (char *)list );
119 file_read( const char *path, struct berval *bv )
125 if (( fp = fopen( path, "r" )) == NULL ) {
130 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
136 bv->bv_len = ftell( fp );
138 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
144 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
150 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
154 if ( (ber_len_t) rlen != bv->bv_len ) {
160 return( bv->bv_len );
168 const char *prompt3 )
170 static char buf[256];
174 struct berval **bvals;
180 get_line( buf, sizeof(buf), stdin, prompt1 );
181 tmp.mod_op = atoi( buf );
183 if ( tmp.mod_op == -1 || buf[0] == '\0' )
187 get_line( buf, sizeof(buf), stdin, prompt2 );
188 if ( buf[0] == '\0' )
190 tmp.mod_type = strdup( buf );
192 tmp.mod_values = get_list( prompt3 );
194 if ( tmp.mod_values != NULL ) {
197 for ( i = 0; tmp.mod_values[i] != NULL; ++i )
199 bvals = (struct berval **)calloc( i + 1,
200 sizeof( struct berval *));
201 for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
202 bvals[i] = (struct berval *)malloc(
203 sizeof( struct berval ));
204 if ( strncmp( tmp.mod_values[i], "{FILE}",
206 if ( file_read( tmp.mod_values[i] + 6,
211 bvals[i]->bv_val = tmp.mod_values[i];
213 strlen( tmp.mod_values[i] );
216 tmp.mod_bvalues = bvals;
217 tmp.mod_op |= LDAP_MOD_BVALUES;
220 if ( result == NULL )
221 result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
223 result = (LDAPMod **) realloc( result,
224 sizeof(LDAPMod *) * (num + 1) );
226 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
227 *(result[num]) = tmp; /* struct copy */
230 if ( result == NULL )
232 result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
240 bind_prompt( LDAP *ld,
241 LDAP_CONST char *url,
242 ber_tag_t request, ber_int_t msgid,
245 static char dn[256], passwd[256];
248 printf("rebind for request=%ld msgid=%ld url=%s\n",
249 request, (long) msgid, url );
251 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
252 get_line( dn, sizeof(dn), stdin,
253 "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
254 if (( authmethod = atoi( dn )) == 3 ) {
255 authmethod = LDAP_AUTH_KRBV4;
259 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
260 authmethod = LDAP_AUTH_SIMPLE;
261 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
263 get_line( dn, sizeof(dn), stdin, "re-bind dn? " );
264 strcat( dn, dnsuffix );
266 if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
267 get_line( passwd, sizeof(passwd), stdin,
268 "re-bind password? " );
273 return ldap_bind_s( ld, dn, passwd, authmethod);
278 main( int argc, char **argv )
281 int i, c, port, errflg, method, id, msgtype;
282 char line[256], command1, command2, command3;
283 char passwd[64], dn[256], rdn[64], attr[64], value[256];
284 char filter[256], *host, **types;
286 char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
287 int bound, all, scope, attrsonly;
289 LDAPMod **mods, **attrs;
290 struct timeval timeout;
291 char *copyfname = NULL;
300 while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
304 ldap_debug = atoi( optarg );
306 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
307 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
311 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
324 port = atoi( optarg );
327 case 't': /* copy ber's to given file */
328 copyfname = strdup( optarg );
329 /* copyoptions = LBER_TO_FILE; */
332 case 'T': /* only output ber's to given file */
333 copyfname = strdup( optarg );
334 /* copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
342 if ( host == NULL && optind == argc - 1 ) {
343 host = argv[ optind ];
347 if ( errflg || optind < argc - 1 ) {
348 fprintf( stderr, usage, argv[ 0 ] );
349 exit( EXIT_FAILURE );
352 printf( "ldap_init( %s, %d )\n",
353 host == NULL ? "(null)" : host, port );
355 ld = ldap_init( host, port );
358 perror( "ldap_init" );
359 exit( EXIT_FAILURE );
362 if ( copyfname != NULL ) {
363 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
366 exit ( EXIT_FAILURE );
368 ld->ld_sb->sb_options = copyoptions;
375 (void) memset( line, '\0', sizeof(line) );
376 while ( get_line( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
381 switch ( command1 ) {
382 case 'a': /* add or abandon */
383 switch ( command2 ) {
385 get_line( dn, sizeof(dn), stdin, "dn? " );
386 strcat( dn, dnsuffix );
387 if ( (attrs = get_modlist( NULL, "attr? ",
388 "value? " )) == NULL )
390 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
391 ldap_perror( ld, "ldap_add" );
393 printf( "Add initiated with id %d\n",
397 case 'b': /* abandon */
398 get_line( line, sizeof(line), stdin, "msgid? " );
400 if ( ldap_abandon( ld, id ) != 0 )
401 ldap_perror( ld, "ldap_abandon" );
403 printf( "Abandon successful\n" );
406 printf( "Possibilities: [ad]d, [ab]ort\n" );
410 case 'b': /* asynch bind */
411 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
412 get_line( line, sizeof(line), stdin,
413 "method (0->simple, 1->krbv41, 2->krbv42)? " );
414 method = atoi( line ) | 0x80;
415 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
416 method = LDAP_AUTH_SIMPLE;
417 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
418 get_line( dn, sizeof(dn), stdin, "dn? " );
419 strcat( dn, dnsuffix );
421 if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
422 get_line( passwd, sizeof(passwd), stdin,
427 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
428 fprintf( stderr, "ldap_bind failed\n" );
429 ldap_perror( ld, "ldap_bind" );
431 printf( "Bind initiated\n" );
436 case 'B': /* synch bind */
437 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
438 get_line( line, sizeof(line), stdin,
439 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
440 method = atoi( line );
442 method = LDAP_AUTH_KRBV4;
444 method = method | 0x80;
445 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
446 method = LDAP_AUTH_SIMPLE;
447 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
448 get_line( dn, sizeof(dn), stdin, "dn? " );
449 strcat( dn, dnsuffix );
452 get_line( passwd, sizeof(passwd), stdin,
457 if ( ldap_bind_s( ld, dn, passwd, method ) !=
459 fprintf( stderr, "ldap_bind_s failed\n" );
460 ldap_perror( ld, "ldap_bind_s" );
462 printf( "Bind successful\n" );
467 case 'c': /* compare */
468 get_line( dn, sizeof(dn), stdin, "dn? " );
469 strcat( dn, dnsuffix );
470 get_line( attr, sizeof(attr), stdin, "attr? " );
471 get_line( value, sizeof(value), stdin, "value? " );
473 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
474 ldap_perror( ld, "ldap_compare" );
476 printf( "Compare initiated with id %d\n", id );
479 case 'd': /* turn on debugging */
481 get_line( line, sizeof(line), stdin, "debug level? " );
482 ldap_debug = atoi( line );
484 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
485 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
489 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
493 case 'E': /* explode a dn */
494 get_line( line, sizeof(line), stdin, "dn? " );
495 exdn = ldap_explode_dn( line, 0 );
496 for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
497 printf( "\t%s\n", exdn[i] );
501 case 'g': /* set next msgid */
502 get_line( line, sizeof(line), stdin, "msgid? " );
503 ld->ld_msgid = atoi( line );
506 case 'v': /* set version number */
507 get_line( line, sizeof(line), stdin, "version? " );
508 ld->ld_version = atoi( line );
511 case 'm': /* modify or modifyrdn */
512 if ( strncmp( line, "modify", 4 ) == 0 ) {
513 get_line( dn, sizeof(dn), stdin, "dn? " );
514 strcat( dn, dnsuffix );
515 if ( (mods = get_modlist(
516 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
517 "attribute type? ", "attribute value? " ))
520 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
521 ldap_perror( ld, "ldap_modify" );
523 printf( "Modify initiated with id %d\n",
525 } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
526 get_line( dn, sizeof(dn), stdin, "dn? " );
527 strcat( dn, dnsuffix );
528 get_line( rdn, sizeof(rdn), stdin, "newrdn? " );
529 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
530 ldap_perror( ld, "ldap_modrdn" );
532 printf( "Modrdn initiated with id %d\n",
535 printf( "Possibilities: [modi]fy, [modr]dn\n" );
541 exit( EXIT_SUCCESS );
544 case 'r': /* result or remove */
545 switch ( command3 ) {
546 case 's': /* result */
547 get_line( line, sizeof(line), stdin,
548 "msgid (-1=>any)? " );
549 if ( line[0] == '\0' )
553 get_line( line, sizeof(line), stdin,
554 "all (0=>any, 1=>all)? " );
555 if ( line[0] == '\0' )
559 if (( msgtype = ldap_result( ld, id, all,
560 &timeout, &res )) < 1 ) {
561 ldap_perror( ld, "ldap_result" );
564 printf( "\nresult: msgtype %d msgid %d\n",
565 msgtype, res->lm_msgid );
566 handle_result( ld, res );
570 case 'm': /* remove */
571 get_line( dn, sizeof(dn), stdin, "dn? " );
572 strcat( dn, dnsuffix );
573 if ( (id = ldap_delete( ld, dn )) == -1 )
574 ldap_perror( ld, "ldap_delete" );
576 printf( "Remove initiated with id %d\n",
581 printf( "Possibilities: [rem]ove, [res]ult\n" );
586 case 's': /* search */
587 get_line( dn, sizeof(dn), stdin, "searchbase? " );
588 strcat( dn, dnsuffix );
589 get_line( line, sizeof(line), stdin,
590 "scope (0=baseObject, 1=oneLevel, 2=subtree, 3=children)? " );
591 scope = atoi( line );
592 get_line( filter, sizeof(filter), stdin,
593 "search filter (e.g. sn=jones)? " );
594 types = get_list( "attrs to return? " );
595 get_line( line, sizeof(line), stdin,
596 "attrsonly (0=attrs&values, 1=attrs only)? " );
597 attrsonly = atoi( line );
599 if (( id = ldap_search( ld, dn, scope, filter,
600 types, attrsonly )) == -1 ) {
601 ldap_perror( ld, "ldap_search" );
603 printf( "Search initiated with id %d\n", id );
608 case 't': /* set timeout value */
609 get_line( line, sizeof(line), stdin, "timeout? " );
610 timeout.tv_sec = atoi( line );
613 case 'p': /* parse LDAP URL */
614 get_line( line, sizeof(line), stdin, "LDAP URL? " );
615 if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
616 fprintf( stderr, "ldap_url_parse: error %d\n", i );
618 printf( "\t host: " );
619 if ( ludp->lud_host == NULL ) {
620 printf( "DEFAULT\n" );
622 printf( "<%s>\n", ludp->lud_host );
624 printf( "\t port: " );
625 if ( ludp->lud_port == 0 ) {
626 printf( "DEFAULT\n" );
628 printf( "%d\n", ludp->lud_port );
630 printf( "\t dn: <%s>\n", ludp->lud_dn );
631 printf( "\t attrs:" );
632 if ( ludp->lud_attrs == NULL ) {
635 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
636 printf( " <%s>", ludp->lud_attrs[ i ] );
639 printf( "\n\t scope: %s\n",
640 ludp->lud_scope == LDAP_SCOPE_BASE ? "baseObject"
641 : ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "oneLevel"
642 : ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "subtree"
643 #ifdef LDAP_SCOPE_SUBORDINATE
644 : ludp->lud_scope == LDAP_SCOPE_SUBORDINATE ? "children"
647 printf( "\tfilter: <%s>\n", ludp->lud_filter );
648 ldap_free_urldesc( ludp );
652 case 'n': /* set dn suffix, for convenience */
653 get_line( line, sizeof(line), stdin, "DN suffix? " );
654 strcpy( dnsuffix, line );
657 case 'o': /* set ldap options */
658 get_line( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
659 ld->ld_deref = atoi( line );
660 get_line( line, sizeof(line), stdin, "timelimit?" );
661 ld->ld_timelimit = atoi( line );
662 get_line( line, sizeof(line), stdin, "sizelimit?" );
663 ld->ld_sizelimit = atoi( line );
665 LDAP_BOOL_ZERO(&ld->ld_options);
667 get_line( line, sizeof(line), stdin,
668 "Recognize and chase referrals (0=no, 1=yes)?" );
669 if ( atoi( line ) != 0 ) {
670 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
671 get_line( line, sizeof(line), stdin,
672 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
673 if ( atoi( line ) != 0 ) {
674 ldap_set_rebind_proc( ld, bind_prompt, NULL );
681 "Commands: [ad]d [ab]andon [b]ind\n"
682 " [B]ind async [c]ompare\n"
683 " [modi]fy [modr]dn [rem]ove\n"
684 " [res]ult [s]earch [q]uit/unbind\n\n"
685 " [d]ebug set ms[g]id\n"
686 " d[n]suffix [t]imeout [v]ersion\n"
688 " [E]xplode dn [p]arse LDAP URL\n" );
692 printf( "Invalid command. Type ? for help.\n" );
696 (void) memset( line, '\0', sizeof(line) );
703 handle_result( LDAP *ld, LDAPMessage *lm )
705 switch ( lm->lm_msgtype ) {
706 case LDAP_RES_COMPARE:
707 printf( "Compare result\n" );
708 print_ldap_result( ld, lm, "compare" );
711 case LDAP_RES_SEARCH_RESULT:
712 printf( "Search result\n" );
713 print_ldap_result( ld, lm, "search" );
716 case LDAP_RES_SEARCH_ENTRY:
717 printf( "Search entry\n" );
718 print_search_entry( ld, lm );
722 printf( "Add result\n" );
723 print_ldap_result( ld, lm, "add" );
726 case LDAP_RES_DELETE:
727 printf( "Delete result\n" );
728 print_ldap_result( ld, lm, "delete" );
731 case LDAP_RES_MODRDN:
732 printf( "ModRDN result\n" );
733 print_ldap_result( ld, lm, "modrdn" );
737 printf( "Bind result\n" );
738 print_ldap_result( ld, lm, "bind" );
742 printf( "Unknown result type 0x%lx\n",
743 (unsigned long) lm->lm_msgtype );
744 print_ldap_result( ld, lm, "unknown" );
749 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
751 ldap_result2error( ld, lm, 1 );
752 ldap_perror( ld, s );
754 if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
755 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
756 if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
757 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
762 print_search_entry( LDAP *ld, LDAPMessage *res )
766 for ( e = ldap_first_entry( ld, res ); e != NULL;
767 e = ldap_next_entry( ld, e ) )
769 BerElement *ber = NULL;
772 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
775 dn = ldap_get_dn( ld, e );
776 printf( "\tDN: %s\n", dn );
778 ufn = ldap_dn2ufn( dn );
779 printf( "\tUFN: %s\n", ufn );
784 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
785 a = ldap_next_attribute( ld, e, ber ) )
787 struct berval **vals;
789 printf( "\t\tATTR: %s\n", a );
790 if ( (vals = ldap_get_values_len( ld, e, a ))
792 printf( "\t\t\t(no values)\n" );
795 for ( i = 0; vals[i] != NULL; i++ ) {
799 for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
800 if ( !isascii( vals[i]->bv_val[j] ) ) {
806 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
807 #ifdef BPRINT_NONASCII
808 ber_bprint( vals[i]->bv_val,
810 #endif /* BPRINT_NONASCII */
813 printf( "\t\t\tlength (%ld) %s\n",
814 vals[i]->bv_len, vals[i]->bv_val );
816 ber_bvecfree( vals );
825 if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
826 || res->lm_chain != NULL )
827 print_ldap_result( ld, res, "search" );