2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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 )
62 fputs(prompt, stdout);
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,
209 for ( i = 0; i<num; i++ )
215 bvals[i]->bv_val = tmp.mod_values[i];
217 strlen( tmp.mod_values[i] );
220 tmp.mod_bvalues = bvals;
221 tmp.mod_op |= LDAP_MOD_BVALUES;
224 if ( result == NULL )
225 result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
227 result = (LDAPMod **) realloc( result,
228 sizeof(LDAPMod *) * (num + 1) );
230 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
231 *(result[num]) = tmp; /* struct copy */
234 if ( result == NULL )
236 result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
244 bind_prompt( LDAP *ld,
245 LDAP_CONST char *url,
246 ber_tag_t request, ber_int_t msgid,
249 static char dn[256], passwd[256];
252 printf("rebind for request=%ld msgid=%ld url=%s\n",
253 request, (long) msgid, url );
255 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
256 get_line( dn, sizeof(dn), stdin,
257 "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
258 if (( authmethod = atoi( dn )) == 3 ) {
259 authmethod = LDAP_AUTH_KRBV4;
263 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
264 authmethod = LDAP_AUTH_SIMPLE;
265 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
267 get_line( dn, sizeof(dn), stdin, "re-bind dn? " );
268 strcat( dn, dnsuffix );
270 if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
271 get_line( passwd, sizeof(passwd), stdin,
272 "re-bind password? " );
277 return ldap_bind_s( ld, dn, passwd, authmethod);
282 main( int argc, char **argv )
285 int i, c, port, errflg, method, id, msgtype;
286 char line[256], command1, command2, command3;
287 char passwd[64], dn[256], rdn[64], attr[64], value[256];
288 char filter[256], *host, **types;
290 char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
291 int bound, all, scope, attrsonly;
293 LDAPMod **mods, **attrs;
294 struct timeval timeout;
295 char *copyfname = NULL;
304 while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
308 ldap_debug = atoi( optarg );
310 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
311 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
315 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
328 port = atoi( optarg );
331 case 't': /* copy ber's to given file */
332 copyfname = strdup( optarg );
333 /* copyoptions = LBER_TO_FILE; */
336 case 'T': /* only output ber's to given file */
337 copyfname = strdup( optarg );
338 /* copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
346 if ( host == NULL && optind == argc - 1 ) {
347 host = argv[ optind ];
351 if ( errflg || optind < argc - 1 ) {
352 fprintf( stderr, usage, argv[ 0 ] );
353 exit( EXIT_FAILURE );
356 printf( "ldap_init( %s, %d )\n",
357 host == NULL ? "(null)" : host, port );
359 ld = ldap_init( host, port );
362 perror( "ldap_init" );
363 exit( EXIT_FAILURE );
366 if ( copyfname != NULL ) {
367 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
370 exit ( EXIT_FAILURE );
372 ld->ld_sb->sb_options = copyoptions;
379 (void) memset( line, '\0', sizeof(line) );
380 while ( get_line( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
385 switch ( command1 ) {
386 case 'a': /* add or abandon */
387 switch ( command2 ) {
389 get_line( dn, sizeof(dn), stdin, "dn? " );
390 strcat( dn, dnsuffix );
391 if ( (attrs = get_modlist( NULL, "attr? ",
392 "value? " )) == NULL )
394 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
395 ldap_perror( ld, "ldap_add" );
397 printf( "Add initiated with id %d\n",
401 case 'b': /* abandon */
402 get_line( line, sizeof(line), stdin, "msgid? " );
404 if ( ldap_abandon( ld, id ) != 0 )
405 ldap_perror( ld, "ldap_abandon" );
407 printf( "Abandon successful\n" );
410 printf( "Possibilities: [ad]d, [ab]ort\n" );
414 case 'b': /* asynch bind */
415 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
416 get_line( line, sizeof(line), stdin,
417 "method (0->simple, 1->krbv41, 2->krbv42)? " );
418 method = atoi( line ) | 0x80;
419 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
420 method = LDAP_AUTH_SIMPLE;
421 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
422 get_line( dn, sizeof(dn), stdin, "dn? " );
423 strcat( dn, dnsuffix );
425 if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
426 get_line( passwd, sizeof(passwd), stdin,
431 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
432 fprintf( stderr, "ldap_bind failed\n" );
433 ldap_perror( ld, "ldap_bind" );
435 printf( "Bind initiated\n" );
440 case 'B': /* synch bind */
441 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
442 get_line( line, sizeof(line), stdin,
443 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
444 method = atoi( line );
446 method = LDAP_AUTH_KRBV4;
448 method = method | 0x80;
449 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
450 method = LDAP_AUTH_SIMPLE;
451 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
452 get_line( dn, sizeof(dn), stdin, "dn? " );
453 strcat( dn, dnsuffix );
456 get_line( passwd, sizeof(passwd), stdin,
461 if ( ldap_bind_s( ld, dn, passwd, method ) !=
463 fprintf( stderr, "ldap_bind_s failed\n" );
464 ldap_perror( ld, "ldap_bind_s" );
466 printf( "Bind successful\n" );
471 case 'c': /* compare */
472 get_line( dn, sizeof(dn), stdin, "dn? " );
473 strcat( dn, dnsuffix );
474 get_line( attr, sizeof(attr), stdin, "attr? " );
475 get_line( value, sizeof(value), stdin, "value? " );
477 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
478 ldap_perror( ld, "ldap_compare" );
480 printf( "Compare initiated with id %d\n", id );
483 case 'd': /* turn on debugging */
485 get_line( line, sizeof(line), stdin, "debug level? " );
486 ldap_debug = atoi( line );
488 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
489 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
493 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
497 case 'E': /* explode a dn */
498 get_line( line, sizeof(line), stdin, "dn? " );
499 exdn = ldap_explode_dn( line, 0 );
500 for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
501 printf( "\t%s\n", exdn[i] );
505 case 'g': /* set next msgid */
506 get_line( line, sizeof(line), stdin, "msgid? " );
507 ld->ld_msgid = atoi( line );
510 case 'v': /* set version number */
511 get_line( line, sizeof(line), stdin, "version? " );
512 ld->ld_version = atoi( line );
515 case 'm': /* modify or modifyrdn */
516 if ( strncmp( line, "modify", 4 ) == 0 ) {
517 get_line( dn, sizeof(dn), stdin, "dn? " );
518 strcat( dn, dnsuffix );
519 if ( (mods = get_modlist(
520 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
521 "attribute type? ", "attribute value? " ))
524 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
525 ldap_perror( ld, "ldap_modify" );
527 printf( "Modify initiated with id %d\n",
529 } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
530 get_line( dn, sizeof(dn), stdin, "dn? " );
531 strcat( dn, dnsuffix );
532 get_line( rdn, sizeof(rdn), stdin, "newrdn? " );
533 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
534 ldap_perror( ld, "ldap_modrdn" );
536 printf( "Modrdn initiated with id %d\n",
539 printf( "Possibilities: [modi]fy, [modr]dn\n" );
545 exit( EXIT_SUCCESS );
548 case 'r': /* result or remove */
549 switch ( command3 ) {
550 case 's': /* result */
551 get_line( line, sizeof(line), stdin,
552 "msgid (-1=>any)? " );
553 if ( line[0] == '\0' )
557 get_line( line, sizeof(line), stdin,
558 "all (0=>any, 1=>all)? " );
559 if ( line[0] == '\0' )
563 if (( msgtype = ldap_result( ld, id, all,
564 &timeout, &res )) < 1 ) {
565 ldap_perror( ld, "ldap_result" );
568 printf( "\nresult: msgtype %d msgid %d\n",
569 msgtype, res->lm_msgid );
570 handle_result( ld, res );
574 case 'm': /* remove */
575 get_line( dn, sizeof(dn), stdin, "dn? " );
576 strcat( dn, dnsuffix );
577 if ( (id = ldap_delete( ld, dn )) == -1 )
578 ldap_perror( ld, "ldap_delete" );
580 printf( "Remove initiated with id %d\n",
585 printf( "Possibilities: [rem]ove, [res]ult\n" );
590 case 's': /* search */
591 get_line( dn, sizeof(dn), stdin, "searchbase? " );
592 strcat( dn, dnsuffix );
593 get_line( line, sizeof(line), stdin,
594 "scope (0=baseObject, 1=oneLevel, 2=subtree, 3=children)? " );
595 scope = atoi( line );
596 get_line( filter, sizeof(filter), stdin,
597 "search filter (e.g. sn=jones)? " );
598 types = get_list( "attrs to return? " );
599 get_line( line, sizeof(line), stdin,
600 "attrsonly (0=attrs&values, 1=attrs only)? " );
601 attrsonly = atoi( line );
603 if (( id = ldap_search( ld, dn, scope, filter,
604 types, attrsonly )) == -1 ) {
605 ldap_perror( ld, "ldap_search" );
607 printf( "Search initiated with id %d\n", id );
612 case 't': /* set timeout value */
613 get_line( line, sizeof(line), stdin, "timeout? " );
614 timeout.tv_sec = atoi( line );
617 case 'p': /* parse LDAP URL */
618 get_line( line, sizeof(line), stdin, "LDAP URL? " );
619 if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
620 fprintf( stderr, "ldap_url_parse: error %d\n", i );
622 printf( "\t host: " );
623 if ( ludp->lud_host == NULL ) {
624 printf( "DEFAULT\n" );
626 printf( "<%s>\n", ludp->lud_host );
628 printf( "\t port: " );
629 if ( ludp->lud_port == 0 ) {
630 printf( "DEFAULT\n" );
632 printf( "%d\n", ludp->lud_port );
634 printf( "\t dn: <%s>\n", ludp->lud_dn );
635 printf( "\t attrs:" );
636 if ( ludp->lud_attrs == NULL ) {
639 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
640 printf( " <%s>", ludp->lud_attrs[ i ] );
643 printf( "\n\t scope: %s\n",
644 ludp->lud_scope == LDAP_SCOPE_BASE ? "baseObject"
645 : ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "oneLevel"
646 : ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "subtree"
647 #ifdef LDAP_SCOPE_SUBORDINATE
648 : ludp->lud_scope == LDAP_SCOPE_SUBORDINATE ? "children"
651 printf( "\tfilter: <%s>\n", ludp->lud_filter );
652 ldap_free_urldesc( ludp );
656 case 'n': /* set dn suffix, for convenience */
657 get_line( line, sizeof(line), stdin, "DN suffix? " );
658 strcpy( dnsuffix, line );
661 case 'o': /* set ldap options */
662 get_line( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
663 ld->ld_deref = atoi( line );
664 get_line( line, sizeof(line), stdin, "timelimit?" );
665 ld->ld_timelimit = atoi( line );
666 get_line( line, sizeof(line), stdin, "sizelimit?" );
667 ld->ld_sizelimit = atoi( line );
669 LDAP_BOOL_ZERO(&ld->ld_options);
671 get_line( line, sizeof(line), stdin,
672 "Recognize and chase referrals (0=no, 1=yes)?" );
673 if ( atoi( line ) != 0 ) {
674 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
675 get_line( line, sizeof(line), stdin,
676 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
677 if ( atoi( line ) != 0 ) {
678 ldap_set_rebind_proc( ld, bind_prompt, NULL );
685 "Commands: [ad]d [ab]andon [b]ind\n"
686 " [B]ind async [c]ompare\n"
687 " [modi]fy [modr]dn [rem]ove\n"
688 " [res]ult [s]earch [q]uit/unbind\n\n"
689 " [d]ebug set ms[g]id\n"
690 " d[n]suffix [t]imeout [v]ersion\n"
692 " [E]xplode dn [p]arse LDAP URL\n" );
696 printf( "Invalid command. Type ? for help.\n" );
700 (void) memset( line, '\0', sizeof(line) );
707 handle_result( LDAP *ld, LDAPMessage *lm )
709 switch ( lm->lm_msgtype ) {
710 case LDAP_RES_COMPARE:
711 printf( "Compare result\n" );
712 print_ldap_result( ld, lm, "compare" );
715 case LDAP_RES_SEARCH_RESULT:
716 printf( "Search result\n" );
717 print_ldap_result( ld, lm, "search" );
720 case LDAP_RES_SEARCH_ENTRY:
721 printf( "Search entry\n" );
722 print_search_entry( ld, lm );
726 printf( "Add result\n" );
727 print_ldap_result( ld, lm, "add" );
730 case LDAP_RES_DELETE:
731 printf( "Delete result\n" );
732 print_ldap_result( ld, lm, "delete" );
735 case LDAP_RES_MODRDN:
736 printf( "ModRDN result\n" );
737 print_ldap_result( ld, lm, "modrdn" );
741 printf( "Bind result\n" );
742 print_ldap_result( ld, lm, "bind" );
746 printf( "Unknown result type 0x%lx\n",
747 (unsigned long) lm->lm_msgtype );
748 print_ldap_result( ld, lm, "unknown" );
753 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
755 ldap_result2error( ld, lm, 1 );
756 ldap_perror( ld, s );
758 if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
759 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
760 if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
761 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
766 print_search_entry( LDAP *ld, LDAPMessage *res )
770 for ( e = ldap_first_entry( ld, res ); e != NULL;
771 e = ldap_next_entry( ld, e ) )
773 BerElement *ber = NULL;
776 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
779 dn = ldap_get_dn( ld, e );
780 printf( "\tDN: %s\n", dn );
782 ufn = ldap_dn2ufn( dn );
783 printf( "\tUFN: %s\n", ufn );
788 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
789 a = ldap_next_attribute( ld, e, ber ) )
791 struct berval **vals;
793 printf( "\t\tATTR: %s\n", a );
794 if ( (vals = ldap_get_values_len( ld, e, a ))
796 printf( "\t\t\t(no values)\n" );
799 for ( i = 0; vals[i] != NULL; i++ ) {
803 for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
804 if ( !isascii( vals[i]->bv_val[j] ) ) {
810 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
811 #ifdef BPRINT_NONASCII
812 ber_bprint( vals[i]->bv_val,
814 #endif /* BPRINT_NONASCII */
817 printf( "\t\t\tlength (%ld) %s\n",
818 vals[i]->bv_len, vals[i]->bv_val );
820 ber_bvecfree( vals );
829 if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
830 || res->lm_chain != NULL )
831 print_ldap_result( ld, res, "search" );