2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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 authmethod = LDAP_AUTH_SIMPLE;
257 get_line( dn, sizeof(dn), stdin, "re-bind dn? " );
258 strcat( dn, dnsuffix );
260 if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
261 get_line( passwd, sizeof(passwd), stdin,
262 "re-bind password? " );
267 return ldap_bind_s( ld, dn, passwd, authmethod);
272 main( int argc, char **argv )
275 int i, c, port, errflg, method, id, msgtype;
276 char line[256], command1, command2, command3;
277 char passwd[64], dn[256], rdn[64], attr[64], value[256];
278 char filter[256], *host, **types;
280 char *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
281 int bound, all, scope, attrsonly;
283 LDAPMod **mods, **attrs;
284 struct timeval timeout;
285 char *copyfname = NULL;
294 while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
298 ldap_debug = atoi( optarg );
300 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
301 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
305 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
318 port = atoi( optarg );
321 case 't': /* copy ber's to given file */
322 copyfname = strdup( optarg );
323 /* copyoptions = LBER_TO_FILE; */
326 case 'T': /* only output ber's to given file */
327 copyfname = strdup( optarg );
328 /* copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
336 if ( host == NULL && optind == argc - 1 ) {
337 host = argv[ optind ];
341 if ( errflg || optind < argc - 1 ) {
342 fprintf( stderr, usage, argv[ 0 ] );
343 exit( EXIT_FAILURE );
346 printf( "ldap_init( %s, %d )\n",
347 host == NULL ? "(null)" : host, port );
349 ld = ldap_init( host, port );
352 perror( "ldap_init" );
353 exit( EXIT_FAILURE );
356 if ( copyfname != NULL ) {
357 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
360 exit ( EXIT_FAILURE );
362 ld->ld_sb->sb_options = copyoptions;
369 (void) memset( line, '\0', sizeof(line) );
370 while ( get_line( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
375 switch ( command1 ) {
376 case 'a': /* add or abandon */
377 switch ( command2 ) {
379 get_line( dn, sizeof(dn), stdin, "dn? " );
380 strcat( dn, dnsuffix );
381 if ( (attrs = get_modlist( NULL, "attr? ",
382 "value? " )) == NULL )
384 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
385 ldap_perror( ld, "ldap_add" );
387 printf( "Add initiated with id %d\n",
391 case 'b': /* abandon */
392 get_line( line, sizeof(line), stdin, "msgid? " );
394 if ( ldap_abandon( ld, id ) != 0 )
395 ldap_perror( ld, "ldap_abandon" );
397 printf( "Abandon successful\n" );
400 printf( "Possibilities: [ad]d, [ab]ort\n" );
404 case 'b': /* asynch bind */
405 method = LDAP_AUTH_SIMPLE;
406 get_line( dn, sizeof(dn), stdin, "dn? " );
407 strcat( dn, dnsuffix );
409 if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
410 get_line( passwd, sizeof(passwd), stdin,
415 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
416 fprintf( stderr, "ldap_bind failed\n" );
417 ldap_perror( ld, "ldap_bind" );
419 printf( "Bind initiated\n" );
424 case 'B': /* synch bind */
425 method = LDAP_AUTH_SIMPLE;
426 get_line( dn, sizeof(dn), stdin, "dn? " );
427 strcat( dn, dnsuffix );
430 get_line( passwd, sizeof(passwd), stdin,
435 if ( ldap_bind_s( ld, dn, passwd, method ) !=
437 fprintf( stderr, "ldap_bind_s failed\n" );
438 ldap_perror( ld, "ldap_bind_s" );
440 printf( "Bind successful\n" );
445 case 'c': /* compare */
446 get_line( dn, sizeof(dn), stdin, "dn? " );
447 strcat( dn, dnsuffix );
448 get_line( attr, sizeof(attr), stdin, "attr? " );
449 get_line( value, sizeof(value), stdin, "value? " );
451 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
452 ldap_perror( ld, "ldap_compare" );
454 printf( "Compare initiated with id %d\n", id );
457 case 'd': /* turn on debugging */
459 get_line( line, sizeof(line), stdin, "debug level? " );
460 ldap_debug = atoi( line );
462 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
463 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
467 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
471 case 'E': /* explode a dn */
472 get_line( line, sizeof(line), stdin, "dn? " );
473 exdn = ldap_explode_dn( line, 0 );
474 for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
475 printf( "\t%s\n", exdn[i] );
479 case 'g': /* set next msgid */
480 get_line( line, sizeof(line), stdin, "msgid? " );
481 ld->ld_msgid = atoi( line );
484 case 'v': /* set version number */
485 get_line( line, sizeof(line), stdin, "version? " );
486 ld->ld_version = atoi( line );
489 case 'm': /* modify or modifyrdn */
490 if ( strncmp( line, "modify", 4 ) == 0 ) {
491 get_line( dn, sizeof(dn), stdin, "dn? " );
492 strcat( dn, dnsuffix );
493 if ( (mods = get_modlist(
494 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
495 "attribute type? ", "attribute value? " ))
498 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
499 ldap_perror( ld, "ldap_modify" );
501 printf( "Modify initiated with id %d\n",
503 } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
504 get_line( dn, sizeof(dn), stdin, "dn? " );
505 strcat( dn, dnsuffix );
506 get_line( rdn, sizeof(rdn), stdin, "newrdn? " );
507 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
508 ldap_perror( ld, "ldap_modrdn" );
510 printf( "Modrdn initiated with id %d\n",
513 printf( "Possibilities: [modi]fy, [modr]dn\n" );
519 exit( EXIT_SUCCESS );
522 case 'r': /* result or remove */
523 switch ( command3 ) {
524 case 's': /* result */
525 get_line( line, sizeof(line), stdin,
526 "msgid (-1=>any)? " );
527 if ( line[0] == '\0' )
531 get_line( line, sizeof(line), stdin,
532 "all (0=>any, 1=>all)? " );
533 if ( line[0] == '\0' )
537 if (( msgtype = ldap_result( ld, id, all,
538 &timeout, &res )) < 1 ) {
539 ldap_perror( ld, "ldap_result" );
542 printf( "\nresult: msgtype %d msgid %d\n",
543 msgtype, res->lm_msgid );
544 handle_result( ld, res );
548 case 'm': /* remove */
549 get_line( dn, sizeof(dn), stdin, "dn? " );
550 strcat( dn, dnsuffix );
551 if ( (id = ldap_delete( ld, dn )) == -1 )
552 ldap_perror( ld, "ldap_delete" );
554 printf( "Remove initiated with id %d\n",
559 printf( "Possibilities: [rem]ove, [res]ult\n" );
564 case 's': /* search */
565 get_line( dn, sizeof(dn), stdin, "searchbase? " );
566 strcat( dn, dnsuffix );
567 get_line( line, sizeof(line), stdin,
568 "scope (0=baseObject, 1=oneLevel, 2=subtree, 3=children)? " );
569 scope = atoi( line );
570 get_line( filter, sizeof(filter), stdin,
571 "search filter (e.g. sn=jones)? " );
572 types = get_list( "attrs to return? " );
573 get_line( line, sizeof(line), stdin,
574 "attrsonly (0=attrs&values, 1=attrs only)? " );
575 attrsonly = atoi( line );
577 if (( id = ldap_search( ld, dn, scope, filter,
578 types, attrsonly )) == -1 ) {
579 ldap_perror( ld, "ldap_search" );
581 printf( "Search initiated with id %d\n", id );
586 case 't': /* set timeout value */
587 get_line( line, sizeof(line), stdin, "timeout? " );
588 timeout.tv_sec = atoi( line );
591 case 'p': /* parse LDAP URL */
592 get_line( line, sizeof(line), stdin, "LDAP URL? " );
593 if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
594 fprintf( stderr, "ldap_url_parse: error %d\n", i );
596 printf( "\t host: " );
597 if ( ludp->lud_host == NULL ) {
598 printf( "DEFAULT\n" );
600 printf( "<%s>\n", ludp->lud_host );
602 printf( "\t port: " );
603 if ( ludp->lud_port == 0 ) {
604 printf( "DEFAULT\n" );
606 printf( "%d\n", ludp->lud_port );
608 printf( "\t dn: <%s>\n", ludp->lud_dn );
609 printf( "\t attrs:" );
610 if ( ludp->lud_attrs == NULL ) {
613 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
614 printf( " <%s>", ludp->lud_attrs[ i ] );
617 printf( "\n\t scope: %s\n",
618 ludp->lud_scope == LDAP_SCOPE_BASE ? "baseObject"
619 : ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "oneLevel"
620 : ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "subtree"
621 #ifdef LDAP_SCOPE_SUBORDINATE
622 : ludp->lud_scope == LDAP_SCOPE_SUBORDINATE ? "children"
625 printf( "\tfilter: <%s>\n", ludp->lud_filter );
626 ldap_free_urldesc( ludp );
630 case 'n': /* set dn suffix, for convenience */
631 get_line( line, sizeof(line), stdin, "DN suffix? " );
632 strcpy( dnsuffix, line );
635 case 'o': /* set ldap options */
636 get_line( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
637 ld->ld_deref = atoi( line );
638 get_line( line, sizeof(line), stdin, "timelimit?" );
639 ld->ld_timelimit = atoi( line );
640 get_line( line, sizeof(line), stdin, "sizelimit?" );
641 ld->ld_sizelimit = atoi( line );
643 LDAP_BOOL_ZERO(&ld->ld_options);
645 get_line( line, sizeof(line), stdin,
646 "Recognize and chase referrals (0=no, 1=yes)?" );
647 if ( atoi( line ) != 0 ) {
648 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
649 get_line( line, sizeof(line), stdin,
650 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
651 if ( atoi( line ) != 0 ) {
652 ldap_set_rebind_proc( ld, bind_prompt, NULL );
659 "Commands: [ad]d [ab]andon [b]ind\n"
660 " [B]ind async [c]ompare\n"
661 " [modi]fy [modr]dn [rem]ove\n"
662 " [res]ult [s]earch [q]uit/unbind\n\n"
663 " [d]ebug set ms[g]id\n"
664 " d[n]suffix [t]imeout [v]ersion\n"
666 " [E]xplode dn [p]arse LDAP URL\n" );
670 printf( "Invalid command. Type ? for help.\n" );
674 (void) memset( line, '\0', sizeof(line) );
681 handle_result( LDAP *ld, LDAPMessage *lm )
683 switch ( lm->lm_msgtype ) {
684 case LDAP_RES_COMPARE:
685 printf( "Compare result\n" );
686 print_ldap_result( ld, lm, "compare" );
689 case LDAP_RES_SEARCH_RESULT:
690 printf( "Search result\n" );
691 print_ldap_result( ld, lm, "search" );
694 case LDAP_RES_SEARCH_ENTRY:
695 printf( "Search entry\n" );
696 print_search_entry( ld, lm );
700 printf( "Add result\n" );
701 print_ldap_result( ld, lm, "add" );
704 case LDAP_RES_DELETE:
705 printf( "Delete result\n" );
706 print_ldap_result( ld, lm, "delete" );
709 case LDAP_RES_MODRDN:
710 printf( "ModRDN result\n" );
711 print_ldap_result( ld, lm, "modrdn" );
715 printf( "Bind result\n" );
716 print_ldap_result( ld, lm, "bind" );
720 printf( "Unknown result type 0x%lx\n",
721 (unsigned long) lm->lm_msgtype );
722 print_ldap_result( ld, lm, "unknown" );
727 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
729 ldap_result2error( ld, lm, 1 );
730 ldap_perror( ld, s );
732 if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
733 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
734 if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
735 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
740 print_search_entry( LDAP *ld, LDAPMessage *res )
744 for ( e = ldap_first_entry( ld, res ); e != NULL;
745 e = ldap_next_entry( ld, e ) )
747 BerElement *ber = NULL;
750 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
753 dn = ldap_get_dn( ld, e );
754 printf( "\tDN: %s\n", dn );
756 ufn = ldap_dn2ufn( dn );
757 printf( "\tUFN: %s\n", ufn );
762 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
763 a = ldap_next_attribute( ld, e, ber ) )
765 struct berval **vals;
767 printf( "\t\tATTR: %s\n", a );
768 if ( (vals = ldap_get_values_len( ld, e, a ))
770 printf( "\t\t\t(no values)\n" );
773 for ( i = 0; vals[i] != NULL; i++ ) {
777 for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
778 if ( !isascii( vals[i]->bv_val[j] ) ) {
784 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
785 #ifdef BPRINT_NONASCII
786 ber_bprint( vals[i]->bv_val,
788 #endif /* BPRINT_NONASCII */
791 printf( "\t\t\tlength (%ld) %s\n",
792 vals[i]->bv_len, vals[i]->bv_val );
794 ber_bvecfree( vals );
803 if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
804 || res->lm_chain != NULL )
805 print_ldap_result( ld, res, "search" );