2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
9 #include <sys/socket.h>
22 static void handle_result( LDAP
*ld
, LDAPMessage
*lm
);
23 static void print_ldap_result( LDAP
*ld
, LDAPMessage
*lm
, char *s
);
24 static void print_search_entry( LDAP
*ld
, LDAPMessage
*res
);
25 static void free_list( char **list
);
27 static void handle_result();
28 static void print_ldap_result();
29 static void print_search_entry();
30 static void free_list();
31 #endif /* NEEDPROTOS */
33 #define NOCACHEERRMSG "don't compile with -DNO_CACHE if you desire local caching"
38 getaline( char *line
, int len
, FILE *fp
, char *prompt
)
42 if ( fgets( line
, len
, fp
) == NULL
)
45 line
[ strlen( line
) - 1 ] = '\0';
51 get_list( char *prompt
)
60 getaline( buf
, sizeof(buf
), stdin
, prompt
);
65 if ( result
== (char **) 0 )
66 result
= (char **) malloc( sizeof(char *) );
68 result
= reallocarray(result
, num
+ 1,
71 result
[num
++] = (char *) strdup( buf
);
73 if ( result
== (char **) 0 )
75 result
= (char **) reallocarray(result
, num
+ 1, sizeof(char *));
83 free_list( char **list
)
88 for ( i
= 0; list
[ i
] != NULL
; ++i
) {
98 file_read( char *path
, struct berval
*bv
)
104 if (( fp
= fopen( path
, "r" )) == NULL
) {
109 if ( fseek( fp
, 0L, SEEK_END
) != 0 ) {
115 bv
->bv_len
= ftell( fp
);
117 if (( bv
->bv_val
= (char *)malloc( bv
->bv_len
)) == NULL
) {
123 if ( fseek( fp
, 0L, SEEK_SET
) != 0 ) {
129 rlen
= fread( bv
->bv_val
, 1, bv
->bv_len
, fp
);
133 if ( rlen
!= bv
->bv_len
) {
139 return( bv
->bv_len
);
141 #endif /* MOD_USE_BVALS */
145 get_modlist( char *prompt1
, char *prompt2
, char *prompt3
)
147 static char buf
[256];
152 struct berval
**bvals
;
153 #endif /* MOD_USE_BVALS */
159 getaline( buf
, sizeof(buf
), stdin
, prompt1
);
160 tmp
.mod_op
= atoi( buf
);
162 if ( tmp
.mod_op
== -1 || buf
[0] == '\0' )
166 getaline( buf
, sizeof(buf
), stdin
, prompt2
);
167 if ( buf
[0] == '\0' )
169 tmp
.mod_type
= strdup( buf
);
171 tmp
.mod_values
= get_list( prompt3
);
173 if ( tmp
.mod_values
!= NULL
) {
176 for ( i
= 0; tmp
.mod_values
[i
] != NULL
; ++i
)
178 bvals
= (struct berval
**)calloc( i
+ 1,
179 sizeof( struct berval
*));
180 for ( i
= 0; tmp
.mod_values
[i
] != NULL
; ++i
) {
181 bvals
[i
] = (struct berval
*)malloc(
182 sizeof( struct berval
));
183 if ( strncmp( tmp
.mod_values
[i
], "{FILE}",
185 if ( file_read( tmp
.mod_values
[i
] + 6,
190 bvals
[i
]->bv_val
= tmp
.mod_values
[i
];
192 strlen( tmp
.mod_values
[i
] );
195 tmp
.mod_bvalues
= bvals
;
196 tmp
.mod_op
|= LDAP_MOD_BVALUES
;
198 #endif /* MOD_USE_BVALS */
200 if ( result
== NULL
)
201 result
= (LDAPMod
**) malloc( sizeof(LDAPMod
*) );
203 result
= (LDAPMod
**) reallocarray(result
, num
+ 1,
206 result
[num
] = (LDAPMod
*) malloc( sizeof(LDAPMod
) );
207 *(result
[num
]) = tmp
; /* struct copy */
210 if ( result
== NULL
)
212 result
= (LDAPMod
**) reallocarray(result
, num
+ 1,
221 bind_prompt( LDAP
*ld
, char **dnp
, char **passwdp
, int *authmethodp
,
224 static char dn
[256], passwd
[256];
228 getaline( dn
, sizeof(dn
), stdin
,
229 "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
230 if (( *authmethodp
= atoi( dn
)) == 3 ) {
231 *authmethodp
= LDAP_AUTH_KRBV4
;
233 *authmethodp
|= 0x80;
236 *authmethodp
= LDAP_AUTH_SIMPLE
;
237 #endif /* KERBEROS */
239 getaline( dn
, sizeof(dn
), stdin
, "re-bind dn? " );
240 strcat( dn
, dnsuffix
);
243 if ( *authmethodp
== LDAP_AUTH_SIMPLE
&& dn
[0] != '\0' ) {
244 getaline( passwd
, sizeof(passwd
), stdin
,
245 "re-bind password? " );
252 return( LDAP_SUCCESS
);
257 main(int argc
, char **argv
)
260 int i
, c
, port
, cldapflg
, errflg
, method
, id
,
261 msgtype
, delrdn
, theInt
, sizelimit
, err
;
262 char line
[256], command1
, command2
, command3
;
263 char passwd
[64], dn
[256], rdn
[64], attr
[64], value
[256];
264 char filter
[256], *host
, **types
;
268 char *usage
= "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
269 int bound
, all
, scope
, attrsonly
;
271 LDAPMod
**mods
, **attrs
;
272 struct timeval timeout
, timelimit
;
273 char *copyfname
= NULL
;
274 int copyoptions
= 0, resultusetimelimit
= 0;
276 struct berval bv
, cred
, *srvcrds
= NULL
;
279 LDAPControl
*ctrls
[2];
284 if (( argv
= get_list( "cmd line arg?" )) == NULL
) {
287 for ( argc
= 0; argv
[ argc
] != NULL
; ++argc
) {
295 cldapflg
= errflg
= 0;
299 while (( c
= getopt( argc
, argv
, "uh:d:s:p:t:T:" )) != -1 ) {
305 printf( "Compile with -DCLDAP for UDP support\n" );
311 ldap_debug
= atoi( optarg
);
312 if ( ldap_debug
& LDAP_DEBUG_PACKETS
) {
313 lber_debug
= ldap_debug
;
316 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
329 port
= atoi( optarg
);
332 #if !defined(MACOS) && !defined(DOS)
333 case 't': /* copy ber's to given file */
334 copyfname
= strdup( optarg
);
335 copyoptions
= LBER_TO_FILE
;
338 case 'T': /* only output ber's to given file */
339 copyfname
= strdup( optarg
);
340 copyoptions
= (LBER_TO_FILE
| LBER_TO_FILE_ONLY
);
349 if ( host
== NULL
&& optind
== argc
- 1 ) {
350 host
= argv
[ optind
];
354 if ( errflg
|| optind
< argc
- 1 ) {
355 fprintf( stderr
, usage
, argv
[ 0 ] );
359 printf( "%s( %s, %d )\n", cldapflg
? "cldap_open" : "ldap_init",
360 host
== NULL
? "(null)" : host
, port
);
364 ld
= cldap_open( host
, port
);
367 ld
= ldap_init( host
, port
);
371 perror( "ldap_init" );
375 #if !defined(MACOS) && !defined(DOS)
376 if ( copyfname
!= NULL
) {
377 if ( (ld
->ld_sb
.sb_fd
= open( copyfname
, O_WRONLY
| O_CREAT
,
382 ld
->ld_sb
.sb_options
= copyoptions
;
389 timelimit
.tv_sec
= 0;
390 timelimit
.tv_usec
= 0;
392 (void) memset( line
, '\0', sizeof(line
) );
393 while ( getaline( line
, sizeof(line
), stdin
, "\ncommand? " ) != NULL
) {
398 switch ( command1
) {
399 case 'a': /* add or abandon */
400 switch ( command2
) {
402 getaline( dn
, sizeof(dn
), stdin
, "dn? " );
403 strcat( dn
, dnsuffix
);
404 if ( (attrs
= get_modlist( NULL
, "attr? ",
405 "value? " )) == NULL
)
407 if (ldap_get_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &i
) == LDAP_SUCCESS
&& i
== LDAP_VERSION3
){
408 if ((err
= ldap_add_ext( ld
, dn
, attrs
, NULL
, NULL
, &id
)) != LDAP_SUCCESS
)
409 printf( "Error in ldap_add_ext: %s\n", ldap_err2string(err
) );
411 printf( "Add initiated with id %d\n", id
);
414 if ( (id
= ldap_add( ld
, dn
, attrs
)) == -1 )
415 ldap_perror( ld
, "ldap_add" );
417 printf( "Add initiated with id %d\n", id
);
422 case 'b': /* abandon */
423 getaline( line
, sizeof(line
), stdin
, "msgid? " );
425 if ( ldap_abandon( ld
, id
) != 0 )
426 ldap_perror( ld
, "ldap_abandon" );
428 printf( "Abandon successful\n" );
431 printf( "Possibilities: [ad]d, [ab]ort\n" );
435 case 'b': /* asynch bind */
437 getaline( line
, sizeof(line
), stdin
,
438 "method (0->simple, 1->krbv41, 2->krbv42)? " );
439 method
= atoi( line
) | 0x80;
441 method
= LDAP_AUTH_SIMPLE
;
442 #endif /* KERBEROS */
443 getaline( dn
, sizeof(dn
), stdin
, "dn? " );
444 strcat( dn
, dnsuffix
);
446 if ( method
== LDAP_AUTH_SIMPLE
&& dn
[0] != '\0' )
447 getaline( passwd
, sizeof(passwd
), stdin
,
452 if ( ldap_bind( ld
, dn
, passwd
, method
) == -1 ) {
453 fprintf( stderr
, "ldap_bind failed\n" );
454 ldap_perror( ld
, "ldap_bind" );
456 printf( "Bind initiated\n" );
461 case 'B': /* synch bind */
463 getaline( line
, sizeof(line
), stdin
,
464 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
465 method
= atoi( line
);
467 method
= LDAP_AUTH_KRBV4
;
469 method
= method
| 0x80;
471 getaline( line
, sizeof(line
), stdin
,
472 "method 0->simple, 1->SASL? ");
473 method
= atoi (line
);
475 method
= LDAP_AUTH_SASL
;
476 getaline( line
, sizeof(line
), stdin
,
477 "mechanism 0->CRAM_MD5, 1->TLS? ");
480 mechanism
= LDAP_SASL_CRAM_MD5
;
483 mechanism
= LDAP_SASL_X511_STRONG
;
486 method
= LDAP_AUTH_SIMPLE
;
489 #endif /* KERBEROS */
490 getaline( dn
, sizeof(dn
), stdin
, "dn? " );
491 strcat( dn
, dnsuffix
);
494 getaline( passwd
, sizeof(passwd
), stdin
,
499 if (method
== LDAP_AUTH_SIMPLE
) {
500 if ( ldap_bind_s( ld
, dn
, passwd
, method
) !=
502 fprintf( stderr
, "ldap_bind_s failed\n" );
503 ldap_perror( ld
, "ldap_bind_s" );
505 printf( "Bind successful\n" );
509 if (strcmp(mechanism
, LDAP_SASL_CRAM_MD5
) == 0){
510 cred
.bv_val
= passwd
;
511 cred
.bv_len
= strlen(passwd
);
513 if ( ldap_sasl_cram_md5_bind_s(ld
, dn
, &cred
, NULL
, NULL
) != LDAP_SUCCESS
){
514 fprintf( stderr
, "ldap_sasl_cram_md5_bind_s failed\n" );
515 ldap_perror( ld
, "ldap_sasl_cram_md5_bind_s" );
517 printf ( "Bind successful\n");
521 if (ldap_sasl_bind_s(ld
, dn
, mechanism
, &cred
, NULL
, NULL
, &srvcrds
) != LDAP_SUCCESS
){
522 fprintf( stderr
, "ldap_sasl_bind_s failed\n" );
523 ldap_perror( ld
, "ldap_sasl_bind_s" );
529 case 'c': /* compare */
530 getaline( dn
, sizeof(dn
), stdin
, "dn? " );
531 strcat( dn
, dnsuffix
);
532 getaline( attr
, sizeof(attr
), stdin
, "attr? " );
533 getaline( value
, sizeof(value
), stdin
, "value? " );
535 if (ldap_get_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &i
) == LDAP_SUCCESS
&& i
== LDAP_VERSION3
){
537 bv
.bv_len
= strlen(value
);
538 if ((err
= ldap_compare_ext( ld
, dn
, attr
, &bv
, NULL
, NULL
, &id
)) != LDAP_SUCCESS
)
539 printf( "Error in ldap_compare_ext: %s\n", ldap_err2string(err
) );
541 printf( "Compare initiated with id %d\n", id
);
543 if ( (id
= ldap_compare( ld
, dn
, attr
, value
)) == -1 )
544 ldap_perror( ld
, "ldap_compare" );
546 printf( "Compare initiated with id %d\n", id
);
550 case 'd': /* turn on debugging */
552 getaline( line
, sizeof(line
), stdin
, "debug level? " );
553 ldap_debug
= atoi( line
);
554 if ( ldap_debug
& LDAP_DEBUG_PACKETS
) {
555 lber_debug
= ldap_debug
;
558 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
562 case 'E': /* explode a dn */
563 getaline( line
, sizeof(line
), stdin
, "dn? " );
564 exdn
= ldap_explode_dn( line
, 0 );
565 for ( i
= 0; exdn
!= NULL
&& exdn
[i
] != NULL
; i
++ ) {
566 printf( "\t%s\n", exdn
[i
] );
570 case 'g': /* set next msgid */
571 getaline( line
, sizeof(line
), stdin
, "msgid? " );
572 ld
->ld_msgid
= atoi( line
);
575 case 'v': /* set version number */
576 getaline( line
, sizeof(line
), stdin
, "version? " );
578 ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &theInt
);
581 case 'm': /* modify or modifyrdn */
582 if ( strncmp( line
, "modify", 4 ) == 0 ) {
583 getaline( dn
, sizeof(dn
), stdin
, "dn? " );
584 strcat( dn
, dnsuffix
);
585 if ( (mods
= get_modlist(
586 "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
587 "attribute type? ", "attribute value? " ))
590 if (ldap_get_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &i
) == LDAP_SUCCESS
&& i
== LDAP_VERSION3
){
591 if ((err
= ldap_modify_ext( ld
, dn
, mods
, NULL
, NULL
, &id
)) != LDAP_SUCCESS
)
592 printf( "Error in ldap_modify_ext: %s\n", ldap_err2string(err
) );
594 printf( "Modify initiated with id %d\n", id
);
597 if ( (id
= ldap_modify( ld
, dn
, mods
)) == -1 )
598 ldap_perror( ld
, "ldap_modify" );
600 printf( "Modify initiated with id %d\n", id
);
602 } else if ( strncmp( line
, "modrdn", 4 ) == 0 ) {
603 getaline( dn
, sizeof(dn
), stdin
, "dn? " );
604 strcat( dn
, dnsuffix
);
605 getaline( rdn
, sizeof(rdn
), stdin
, "newrdn? " );
606 getaline( line
, sizeof(line
), stdin
, "delete old rdn (0=>no, 1=>yes)?");
608 if (ldap_get_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &i
) == LDAP_SUCCESS
&& i
== LDAP_VERSION3
){
609 if ((err
= ldap_rename(ld
, dn
, rdn
, NULL
, delrdn
, NULL
,NULL
, &id
)) != LDAP_SUCCESS
){
610 printf( "Error in ldap_rename (modrdn): %s\n", ldap_err2string(err
));
613 printf( "Modrdn initiated with id %d\n", id
);
616 if ( (id
= ldap_modrdn( ld
, dn
, rdn
, delrdn
)) == -1 )
617 ldap_perror( ld
, "ldap_modrdn" );
619 printf( "Modrdn initiated with id %d\n", id
);
622 printf( "Possibilities: [modi]fy, [modr]dn\n" );
636 case 'r': /* result or remove */
637 switch ( command3
) {
638 case 's': /* result */
639 getaline( line
, sizeof(line
), stdin
,
640 "msgid (-1=>any)? " );
641 if ( line
[0] == '\0' )
645 getaline( line
, sizeof(line
), stdin
,
646 "all (0=>any, 1=>all)? " );
647 if ( line
[0] == '\0' )
652 if (( msgtype
= ldap_result( ld
, id
, all
,
653 resultusetimelimit
? &timelimit
: &timeout
, &res
)) < 1 ) {
654 ldap_perror( ld
, "ldap_result" );
657 printf( "\nresult: msgtype %d msgid %d\n",
658 msgtype
, res
->lm_msgid
);
659 handle_result( ld
, res
);
660 if (all
|| msgtype
== LDAP_RES_SEARCH_RESULT
)
661 resultusetimelimit
= 0;
665 case 'm': /* remove */
666 getaline( dn
, sizeof(dn
), stdin
, "dn? " );
667 strcat( dn
, dnsuffix
);
668 if (ldap_get_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &i
) == LDAP_SUCCESS
&& i
== LDAP_VERSION3
){
669 if ((err
= ldap_delete_ext( ld
, dn
, NULL
, NULL
, &id
)) != LDAP_SUCCESS
)
670 printf( "Error in ldap_delete_ext: %s\n", ldap_err2string(err
) );
672 printf( "Remove initiated with id %d\n", id
);
674 if ( (id
= ldap_delete( ld
, dn
)) == -1 )
675 ldap_perror( ld
, "ldap_delete" );
677 printf( "Remove initiated with id %d\n", id
);
682 printf( "Possibilities: [rem]ove, [res]ult\n" );
687 case 's': /* search */
688 getaline( dn
, sizeof(dn
), stdin
, "searchbase? " );
689 strcat( dn
, dnsuffix
);
690 getaline( line
, sizeof(line
), stdin
,
691 "scope (0=Base, 1=One Level, 2=Subtree)? " );
692 scope
= atoi( line
);
693 getaline( filter
, sizeof(filter
), stdin
,
694 "search filter (e.g. sn=jones)? " );
695 types
= get_list( "attrs to return? " );
696 getaline( line
, sizeof(line
), stdin
,
697 "attrsonly (0=attrs&values, 1=attrs only)? " );
698 attrsonly
= atoi( line
);
702 getaline( line
, sizeof(line
), stdin
,
703 "Requestor DN (for logging)? " );
704 if ( cldap_search_s( ld
, dn
, scope
, filter
, types
,
705 attrsonly
, &res
, line
) != 0 ) {
706 ldap_perror( ld
, "cldap_search_s" );
708 printf( "\nresult: msgid %d\n",
710 handle_result( ld
, res
);
716 if (ldap_get_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &i
) == LDAP_SUCCESS
&& i
== LDAP_VERSION3
){
717 resultusetimelimit
= 1;
718 getaline( line
, sizeof(line
), stdin
,
719 "ldap_search_ext (0=>no, 1=>yes - default: yes)? " );
723 theInt
= atoi( line
);
726 getaline(line
, sizeof(line
), stdin
, "time limit?");
727 timelimit
.tv_sec
= atoi(line
);
728 resultusetimelimit
= 1;
729 getaline(line
, sizeof(line
), stdin
, "size limit?");
730 sizelimit
= atoi(line
);
731 if (( err
= ldap_search_ext(ld
, dn
, scope
, filter
, types
, attrsonly
, NULL
, NULL
,
732 &timelimit
, sizelimit
, &id
)) != LDAP_SUCCESS
){
733 printf( "Error in ldap_search_ext: %s\n", ldap_err2string(err
));
735 printf( "Search initiated with id %d\n", id
);
738 if (( id
= ldap_search( ld
, dn
, scope
, filter
,
739 types
, attrsonly
)) == -1 ) {
740 ldap_perror( ld
, "ldap_search" );
742 printf( "Search initiated with id %d\n", id
);
749 case 't': /* set timeout value */
750 getaline( line
, sizeof(line
), stdin
, "timeout? " );
751 timeout
.tv_sec
= atoi( line
);
754 case 'U': /* set ufn search prefix */
755 getaline( line
, sizeof(line
), stdin
, "ufn prefix? " );
756 ldap_ufn_setprefix( ld
, line
);
759 case 'u': /* user friendly search w/optional timeout */
760 getaline( dn
, sizeof(dn
), stdin
, "ufn? " );
761 strcat( dn
, dnsuffix
);
762 types
= get_list( "attrs to return? " );
763 getaline( line
, sizeof(line
), stdin
,
764 "attrsonly (0=attrs&values, 1=attrs only)? " );
765 attrsonly
= atoi( line
);
767 if ( command2
== 't' ) {
768 id
= ldap_ufn_search_c( ld
, dn
, types
,
769 attrsonly
, &res
, ldap_ufn_timeout
,
772 id
= ldap_ufn_search_s( ld
, dn
, types
,
776 ldap_perror( ld
, "ldap_ufn_search" );
778 printf( "\nresult: err %d\n", id
);
779 handle_result( ld
, res
);
785 case 'l': /* URL search */
786 getaline( line
, sizeof(line
), stdin
,
787 "attrsonly (0=attrs&values, 1=attrs only)? " );
788 attrsonly
= atoi( line
);
789 getaline( line
, sizeof(line
), stdin
, "LDAP URL? " );
790 if (( id
= ldap_url_search( ld
, line
, attrsonly
))
792 ldap_perror( ld
, "ldap_url_search" );
794 printf( "URL search initiated with id %d\n", id
);
798 case 'p': /* parse LDAP URL */
799 getaline( line
, sizeof(line
), stdin
, "LDAP URL? " );
800 if (( i
= ldap_url_parse( line
, &ludp
)) != 0 ) {
801 fprintf( stderr
, "ldap_url_parse: error %d\n", i
);
803 printf( "\t host: " );
804 if ( ludp
->lud_host
== NULL
) {
805 printf( "DEFAULT\n" );
807 printf( "<%s>\n", ludp
->lud_host
);
809 printf( "\t port: " );
810 if ( ludp
->lud_port
== 0 ) {
811 printf( "DEFAULT\n" );
813 printf( "%d\n", ludp
->lud_port
);
815 printf( "\t dn: <%s>\n", ludp
->lud_dn
);
816 printf( "\t attrs:" );
817 if ( ludp
->lud_attrs
== NULL
) {
820 for ( i
= 0; ludp
->lud_attrs
[ i
] != NULL
; ++i
) {
821 printf( " <%s>", ludp
->lud_attrs
[ i
] );
824 printf( "\n\t scope: %s\n", ludp
->lud_scope
== LDAP_SCOPE_UNKNOWN
? "DEFAULT (base)" :
825 ludp
->lud_scope
== LDAP_SCOPE_ONELEVEL
? "ONE" :
826 ludp
->lud_scope
== LDAP_SCOPE_BASE
? "BASE" :
827 ludp
->lud_scope
== LDAP_SCOPE_SUBTREE
? "SUB" : "**invalid**" );
828 printf( "\tfilter: <%s>\n", ludp
->lud_filter
? ludp
->lud_filter
: "NONE");
829 if (ludp
->lud_extensions
){
830 printf("\textensions: \n");
831 for (i
= 0; ludp
->lud_extensions
[i
] != NULL
; i
++)
832 printf("\t\t%s (%s)\n", ludp
->lud_extensions
[i
]->lue_type
,
833 ludp
->lud_extensions
[i
]->lue_iscritical
? "Critical" : "Non critical");
836 ldap_free_urldesc( ludp
);
840 case 'n': /* set dn suffix, for convenience */
841 getaline( line
, sizeof(line
), stdin
, "DN suffix? " );
842 strcpy( dnsuffix
, line
);
845 case 'e': /* enable cache */
847 printf( NOCACHEERRMSG
);
849 getaline( line
, sizeof(line
), stdin
, "Cache timeout (secs)? " );
851 getaline( line
, sizeof(line
), stdin
, "Maximum memory to use (bytes)? " );
852 if ( ldap_enable_cache( ld
, i
, atoi( line
)) == 0 ) {
853 printf( "local cache is on\n" );
855 printf( "ldap_enable_cache failed\n" );
857 #endif /* NO_CACHE */
860 case 'x': /* uncache entry */
862 printf( NOCACHEERRMSG
);
864 getaline( line
, sizeof(line
), stdin
, "DN? " );
865 ldap_uncache_entry( ld
, line
);
866 #endif /* NO_CACHE */
869 case 'X': /* uncache request */
871 printf( NOCACHEERRMSG
);
873 getaline( line
, sizeof(line
), stdin
, "request msgid? " );
874 ldap_uncache_request( ld
, atoi( line
));
875 #endif /* NO_CACHE */
878 case 'o': /* set ldap options */
879 getaline( line
, sizeof(line
), stdin
, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
881 ldap_set_option(ld
, LDAP_OPT_DEREF
, &theInt
);
882 getaline( line
, sizeof(line
), stdin
, "timelimit?" );
884 ldap_set_option(ld
, LDAP_OPT_TIMELIMIT
, &theInt
);
885 getaline( line
, sizeof(line
), stdin
, "sizelimit?" );
887 ldap_set_option(ld
, LDAP_OPT_SIZELIMIT
, &theInt
);
891 #ifdef STR_TRANSLATION
892 getaline( line
, sizeof(line
), stdin
,
893 "Automatic translation of T.61 strings (0=no, 1=yes)?" );
894 if ( atoi( line
) == 0 ) {
895 ld
->ld_lberoptions
&= ~LBER_TRANSLATE_STRINGS
;
897 ld
->ld_lberoptions
|= LBER_TRANSLATE_STRINGS
;
898 #ifdef LDAP_CHARSET_8859
899 getaline( line
, sizeof(line
), stdin
,
900 "Translate to/from ISO-8859 (0=no, 1=yes?" );
901 if ( atoi( line
) != 0 ) {
902 ldap_set_string_translators( ld
,
906 #endif /* LDAP_CHARSET_8859 */
908 #endif /* STR_TRANSLATION */
911 getaline( line
, sizeof(line
), stdin
,
912 "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
913 if ( atoi( line
) != 0 ) {
914 ld
->ld_options
|= LDAP_OPT_DNS
;
916 #endif /* LDAP_DNS */
918 getaline( line
, sizeof(line
), stdin
,
919 "Recognize and chase referrals (0=no, 1=yes)?" );
920 if ( atoi( line
) != 0 ) {
921 theInt
= LDAP_OPT_ON
;
922 getaline( line
, sizeof(line
), stdin
,
923 "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
924 if ( atoi( line
) != 0 ) {
925 ldap_set_option( ld
, LDAP_OPT_REBIND_FN
, bind_prompt
);
928 theInt
= LDAP_OPT_OFF
;
930 ldap_set_option(ld
, LDAP_OPT_REFERRALS
, &theInt
);
933 case 'k': /* Set some controls */
934 getaline( line
, sizeof(line
), stdin
,
935 "Set control: (0 for none, 1 for ManageDSA, 2 for preferredLang, 3 for BAD)?");
939 ldap_set_option(ld
, LDAP_OPT_SERVER_CONTROLS
, NULL
);
942 aCtrl
.ldctl_oid
= "2.16.840.1.113730.3.4.2";
943 aCtrl
.ldctl_iscritical
= 1;
944 aCtrl
.ldctl_value
= NULL
;
945 ldap_set_option(ld
, LDAP_OPT_SERVER_CONTROLS
, ctrls
);
948 getaline( line
, sizeof(line
), stdin
,
949 "Preferred Language Control : lang ?");
950 aCtrl
.ldctl_oid
= "1.3.6.1.4.1.1466.20035";
951 aCtrl
.ldctl_iscritical
= 1;
952 bv
.bv_val
= strdup(line
);
953 bv
.bv_len
= strlen(line
);
954 aCtrl
.ldctl_value
= &bv
;
955 ldap_set_option(ld
, LDAP_OPT_SERVER_CONTROLS
, ctrls
);
958 getaline( line
, sizeof(line
), stdin
,
959 "Bad Control is critical (0=false, 1=true)?");
960 aCtrl
.ldctl_oid
= "1.1.1.1.1.1";
961 aCtrl
.ldctl_iscritical
= atoi(line
);
962 aCtrl
.ldctl_value
= NULL
;
963 ldap_set_option(ld
, LDAP_OPT_SERVER_CONTROLS
, ctrls
);
968 case 'O': /* set cache options */
970 printf( NOCACHEERRMSG
);
972 getaline( line
, sizeof(line
), stdin
, "cache errors (0=smart, 1=never, 2=always)?" );
973 switch( atoi( line
)) {
975 ldap_set_cache_options( ld
, 0 );
978 ldap_set_cache_options( ld
,
979 LDAP_CACHE_OPT_CACHENOERRS
);
982 ldap_set_cache_options( ld
,
983 LDAP_CACHE_OPT_CACHEALLERRS
);
986 printf( "not a valid cache option\n" );
988 #endif /* NO_CACHE */
992 printf( "Commands: [ad]d [ab]andon [b]ind\n" );
993 printf( " [B]ind async [c]ompare [l]URL search\n" );
994 printf( " [modi]fy [modr]dn [rem]ove\n" );
995 printf( " [res]ult [s]earch [q]uit/unbind\n\n" );
996 printf( " [u]fn search [ut]fn search with timeout\n" );
997 printf( " [d]ebug [e]nable cache set ms[g]id\n" );
998 printf( " d[n]suffix [t]imeout [v]ersion\n" );
999 printf( " [U]fn prefix [x]uncache entry [X]uncache request\n" );
1000 printf( " [?]help [o]ptions [O]cache options\n" );
1001 printf( " [E]xplode dn [p]arse LDAP URL\n" );
1005 printf( "Invalid command. Type ? for help.\n" );
1009 (void) memset( line
, '\0', sizeof(line
) );
1016 handle_result( LDAP
*ld
, LDAPMessage
*lm
)
1018 switch ( lm
->lm_msgtype
) {
1019 case LDAP_RES_COMPARE
:
1020 printf( "Compare result\n" );
1021 print_ldap_result( ld
, lm
, "compare" );
1024 case LDAP_RES_SEARCH_RESULT
:
1025 printf( "Search result\n" );
1026 print_ldap_result( ld
, lm
, "search" );
1029 case LDAP_RES_SEARCH_REFERENCE
:
1030 printf( "Search reference\n" );
1031 print_search_entry( ld
, lm
);
1034 case LDAP_RES_SEARCH_ENTRY
:
1035 printf( "Search entry\n" );
1036 print_search_entry( ld
, lm
);
1040 printf( "Add result\n" );
1041 print_ldap_result( ld
, lm
, "add" );
1044 case LDAP_RES_DELETE
:
1045 printf( "Delete result\n" );
1046 print_ldap_result( ld
, lm
, "delete" );
1049 case LDAP_RES_MODIFY
:
1050 printf( "Modify result\n" );
1051 print_ldap_result( ld
, lm
, "modify" );
1054 case LDAP_RES_MODRDN
:
1055 printf( "ModRDN result\n" );
1056 print_ldap_result( ld
, lm
, "modrdn" );
1060 printf( "Bind result\n" );
1061 print_ldap_result( ld
, lm
, "bind" );
1065 printf( "Unknown result type 0x%x\n", lm
->lm_msgtype
);
1066 print_ldap_result( ld
, lm
, "unknown" );
1071 print_ldap_result( LDAP
*ld
, LDAPMessage
*lm
, char *s
)
1075 char *matched
= NULL
, *errMsg
= NULL
, **referrals
= NULL
;
1076 LDAPControl
**srvctrls
= NULL
;
1078 if ((rc
= ldap_parse_result(ld
, lm
, &errCode
, &matched
, &errMsg
, &referrals
, &srvctrls
, 0)) != LDAP_SUCCESS
){
1079 fprintf(stderr
, "%s: error while parsing result (%s)\n", s
, ldap_err2string(rc
));
1084 fprintf(stderr
, "%s: %s\n", s
, ldap_err2string(errCode
));
1085 if (errCode
== LDAP_REFERRAL
){
1086 fprintf(stderr
, "\tReferrals returned: \n");
1087 for (i
= 0; referrals
[i
] != NULL
; i
++)
1088 fprintf(stderr
, "\t\t%s\n", referrals
[i
]);
1090 if (errMsg
&& *errMsg
)
1091 fprintf(stderr
, "\tAdditional info: %s\n", errMsg
);
1093 if (NAME_ERROR(errCode
) && matched
&& *matched
){
1094 fprintf(stderr
, "\tMatched DN: %s\n", matched
);
1097 if (srvctrls
!= NULL
){
1098 fprintf(stderr
, "\tLDAPControls returned: \n");
1099 for (i
=0;srvctrls
[i
] != NULL
; i
++)
1100 fprintf(stderr
, "\t\t%s (%s)\n", srvctrls
[i
]->ldctl_oid
, srvctrls
[i
]->ldctl_iscritical
? "Critical" : "Not critical");
1106 print_search_entry( LDAP
*ld
, LDAPMessage
*res
)
1110 struct berval
**vals
;
1114 for ( e
= ldap_first_message( ld
, res
); e
!= NULLMSG
;
1115 e
= ldap_next_message( ld
, e
) ) {
1116 if ( e
->lm_msgtype
== LDAP_RES_SEARCH_RESULT
)
1119 dn
= ldap_get_dn( ld
, e
);
1120 printf( "\tDN: %s\n", dn
);
1122 ufn
= ldap_dn2ufn( dn
);
1123 printf( "\tUFN: %s\n", ufn
);
1127 if ( e
->lm_msgtype
== LDAP_RES_SEARCH_REFERENCE
){
1128 char **urls
= ldap_get_reference_urls(ld
, e
);
1130 printf("\t\tError with references: %s\n", ldap_err2string(ld
->ld_errno
));
1132 for (i
=0;urls
[i
] != NULL
;i
++)
1133 printf("\t\tURL: %s\n", urls
[i
]);
1136 for ( a
= ldap_first_attribute( ld
, e
, &ber
); a
!= NULL
;
1137 a
= ldap_next_attribute( ld
, e
, ber
) ) {
1138 printf( "\t\tATTR: %s\n", a
);
1139 if ( (vals
= ldap_get_values_len( ld
, e
, a
))
1141 printf( "\t\t\t(no values)\n" );
1143 for ( i
= 0; vals
[i
] != NULL
; i
++ ) {
1147 for ( j
= 0; j
< vals
[i
]->bv_len
; j
++ )
1148 if ( !isascii( vals
[i
]->bv_val
[j
] ) ) {
1154 printf( "\t\t\tlength (%ld) (not ascii)\n", vals
[i
]->bv_len
);
1155 #ifdef BPRINT_NONASCII
1156 lber_bprint( vals
[i
]->bv_val
,
1158 #endif /* BPRINT_NONASCII */
1161 printf( "\t\t\tlength (%ld) %s\n",
1162 vals
[i
]->bv_len
, vals
[i
]->bv_val
);
1164 ber_bvecfree( vals
);
1170 if ( res
->lm_msgtype
== LDAP_RES_SEARCH_RESULT
1171 || res
->lm_chain
!= NULLMSG
)
1172 print_ldap_result( ld
, res
, "search" );