dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / ldap / common / ldaptest.c
blob0f19fc61440de7faac844365fbc4b8e191e937a3
1 /*
2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
3 */
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <sys/time.h>
11 #include <sys/stat.h>
12 #include <sys/file.h>
13 #include <fcntl.h>
14 #include <unistd.h>
16 #include "lber.h"
17 #include "ldap.h"
19 #define MOD_USE_BVALS
21 #ifdef NEEDPROTOS
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 );
26 #else
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"
35 char *dnsuffix;
37 static char *
38 getaline( char *line, int len, FILE *fp, char *prompt )
40 printf(prompt);
42 if ( fgets( line, len, fp ) == NULL )
43 return( NULL );
45 line[ strlen( line ) - 1 ] = '\0';
47 return( line );
50 static char **
51 get_list( char *prompt )
53 static char buf[256];
54 int num;
55 char **result;
57 num = 0;
58 result = (char **) 0;
59 while ( 1 ) {
60 getaline( buf, sizeof(buf), stdin, prompt );
62 if ( *buf == '\0' )
63 break;
65 if ( result == (char **) 0 )
66 result = (char **) malloc( sizeof(char *) );
67 else
68 result = reallocarray(result, num + 1,
69 sizeof(char *));
71 result[num++] = (char *) strdup( buf );
73 if ( result == (char **) 0 )
74 return( NULL );
75 result = (char **) reallocarray(result, num + 1, sizeof(char *));
76 result[num] = NULL;
78 return( result );
82 static void
83 free_list( char **list )
85 int i;
87 if ( list != NULL ) {
88 for ( i = 0; list[ i ] != NULL; ++i ) {
89 free( list[ i ] );
91 free( (char *)list );
96 #ifdef MOD_USE_BVALS
97 static int
98 file_read( char *path, struct berval *bv )
100 FILE *fp;
101 long rlen;
102 int eof;
104 if (( fp = fopen( path, "r" )) == NULL ) {
105 perror( path );
106 return( -1 );
109 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
110 perror( path );
111 fclose( fp );
112 return( -1 );
115 bv->bv_len = ftell( fp );
117 if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
118 perror( "malloc" );
119 fclose( fp );
120 return( -1 );
123 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
124 perror( path );
125 fclose( fp );
126 return( -1 );
129 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
130 eof = feof( fp );
131 fclose( fp );
133 if ( rlen != bv->bv_len ) {
134 perror( path );
135 free( bv->bv_val );
136 return( -1 );
139 return( bv->bv_len );
141 #endif /* MOD_USE_BVALS */
144 static LDAPMod **
145 get_modlist( char *prompt1, char *prompt2, char *prompt3 )
147 static char buf[256];
148 int num;
149 LDAPMod tmp;
150 LDAPMod **result;
151 #ifdef MOD_USE_BVALS
152 struct berval **bvals;
153 #endif /* MOD_USE_BVALS */
155 num = 0;
156 result = NULL;
157 while ( 1 ) {
158 if ( prompt1 ) {
159 getaline( buf, sizeof(buf), stdin, prompt1 );
160 tmp.mod_op = atoi( buf );
162 if ( tmp.mod_op == -1 || buf[0] == '\0' )
163 break;
166 getaline( buf, sizeof(buf), stdin, prompt2 );
167 if ( buf[0] == '\0' )
168 break;
169 tmp.mod_type = strdup( buf );
171 tmp.mod_values = get_list( prompt3 );
172 #ifdef MOD_USE_BVALS
173 if ( tmp.mod_values != NULL ) {
174 int i;
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}",
184 6 ) == 0 ) {
185 if ( file_read( tmp.mod_values[i] + 6,
186 bvals[i] ) < 0 ) {
187 return( NULL );
189 } else {
190 bvals[i]->bv_val = tmp.mod_values[i];
191 bvals[i]->bv_len =
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 *) );
202 else
203 result = (LDAPMod **) reallocarray(result, num + 1,
204 sizeof(LDAPMod *));
206 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
207 *(result[num]) = tmp; /* struct copy */
208 num++;
210 if ( result == NULL )
211 return( NULL );
212 result = (LDAPMod **) reallocarray(result, num + 1,
213 sizeof(LDAPMod *));
214 result[num] = NULL;
216 return( result );
221 bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
222 int freeit )
224 static char dn[256], passwd[256];
226 if ( !freeit ) {
227 #ifdef KERBEROS
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;
232 } else {
233 *authmethodp |= 0x80;
235 #else /* KERBEROS */
236 *authmethodp = LDAP_AUTH_SIMPLE;
237 #endif /* KERBEROS */
239 getaline( dn, sizeof(dn), stdin, "re-bind dn? " );
240 strcat( dn, dnsuffix );
241 *dnp = dn;
243 if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
244 getaline( passwd, sizeof(passwd), stdin,
245 "re-bind password? " );
246 } else {
247 passwd[0] = '\0';
249 *passwdp = passwd;
252 return( LDAP_SUCCESS );
257 main(int argc, char **argv )
259 LDAP *ld;
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;
265 char *mechanism;
267 char **exdn;
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;
270 LDAPMessage *res;
271 LDAPMod **mods, **attrs;
272 struct timeval timeout, timelimit;
273 char *copyfname = NULL;
274 int copyoptions = 0, resultusetimelimit = 0;
275 LDAPURLDesc *ludp;
276 struct berval bv, cred, *srvcrds = NULL;
277 extern char *optarg;
278 extern int optind;
279 LDAPControl *ctrls[2];
280 LDAPControl aCtrl;
283 #ifdef MACOS
284 if (( argv = get_list( "cmd line arg?" )) == NULL ) {
285 exit( 1 );
287 for ( argc = 0; argv[ argc ] != NULL; ++argc ) {
290 #endif /* MACOS */
292 host = NULL;
293 port = LDAP_PORT;
294 dnsuffix = "";
295 cldapflg = errflg = 0;
296 ctrls[0] = &aCtrl;
297 ctrls[1] = NULL;
299 while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
300 switch( c ) {
301 case 'u':
302 #ifdef CLDAP
303 cldapflg++;
304 #else /* CLDAP */
305 printf( "Compile with -DCLDAP for UDP support\n" );
306 #endif /* CLDAP */
307 break;
309 case 'd':
310 #ifdef LDAP_DEBUG
311 ldap_debug = atoi( optarg );
312 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
313 lber_debug = ldap_debug;
315 #else
316 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
317 #endif
318 break;
320 case 'h':
321 host = optarg;
322 break;
324 case 's':
325 dnsuffix = optarg;
326 break;
328 case 'p':
329 port = atoi( optarg );
330 break;
332 #if !defined(MACOS) && !defined(DOS)
333 case 't': /* copy ber's to given file */
334 copyfname = strdup( optarg );
335 copyoptions = LBER_TO_FILE;
336 break;
338 case 'T': /* only output ber's to given file */
339 copyfname = strdup( optarg );
340 copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
341 break;
342 #endif
344 default:
345 ++errflg;
349 if ( host == NULL && optind == argc - 1 ) {
350 host = argv[ optind ];
351 ++optind;
354 if ( errflg || optind < argc - 1 ) {
355 fprintf( stderr, usage, argv[ 0 ] );
356 exit( 1 );
359 printf( "%s( %s, %d )\n", cldapflg ? "cldap_open" : "ldap_init",
360 host == NULL ? "(null)" : host, port );
362 if ( cldapflg ) {
363 #ifdef CLDAP
364 ld = cldap_open( host, port );
365 #endif /* CLDAP */
366 } else {
367 ld = ldap_init( host, port );
370 if ( ld == NULL ) {
371 perror( "ldap_init" );
372 exit(1);
375 #if !defined(MACOS) && !defined(DOS)
376 if ( copyfname != NULL ) {
377 if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
378 0600 )) == -1 ) {
379 perror( copyfname );
380 exit ( 1 );
382 ld->ld_sb.sb_options = copyoptions;
384 #endif
386 bound = 0;
387 timeout.tv_sec = 0;
388 timeout.tv_usec = 0;
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 ) {
394 command1 = line[0];
395 command2 = line[1];
396 command3 = line[2];
398 switch ( command1 ) {
399 case 'a': /* add or abandon */
400 switch ( command2 ) {
401 case 'd': /* add */
402 getaline( dn, sizeof(dn), stdin, "dn? " );
403 strcat( dn, dnsuffix );
404 if ( (attrs = get_modlist( NULL, "attr? ",
405 "value? " )) == NULL )
406 break;
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) );
410 else
411 printf( "Add initiated with id %d\n", id );
413 else {
414 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
415 ldap_perror( ld, "ldap_add" );
416 else
417 printf( "Add initiated with id %d\n", id );
420 break;
422 case 'b': /* abandon */
423 getaline( line, sizeof(line), stdin, "msgid? " );
424 id = atoi( line );
425 if ( ldap_abandon( ld, id ) != 0 )
426 ldap_perror( ld, "ldap_abandon" );
427 else
428 printf( "Abandon successful\n" );
429 break;
430 default:
431 printf( "Possibilities: [ad]d, [ab]ort\n" );
433 break;
435 case 'b': /* asynch bind */
436 #ifdef KERBEROS
437 getaline( line, sizeof(line), stdin,
438 "method (0->simple, 1->krbv41, 2->krbv42)? " );
439 method = atoi( line ) | 0x80;
440 #else /* KERBEROS */
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,
448 "password? " );
449 else
450 passwd[0] = '\0';
452 if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
453 fprintf( stderr, "ldap_bind failed\n" );
454 ldap_perror( ld, "ldap_bind" );
455 } else {
456 printf( "Bind initiated\n" );
457 bound = 1;
459 break;
461 case 'B': /* synch bind */
462 #ifdef KERBEROS
463 getaline( line, sizeof(line), stdin,
464 "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
465 method = atoi( line );
466 if ( method == 3 )
467 method = LDAP_AUTH_KRBV4;
468 else
469 method = method | 0x80;
470 #else /* KERBEROS */
471 getaline( line, sizeof(line), stdin,
472 "method 0->simple, 1->SASL? ");
473 method = atoi (line);
474 if (method == 1){
475 method = LDAP_AUTH_SASL;
476 getaline( line, sizeof(line), stdin,
477 "mechanism 0->CRAM_MD5, 1->TLS? ");
478 theInt = atoi(line);
479 if (theInt == 0){
480 mechanism = LDAP_SASL_CRAM_MD5;
482 else{
483 mechanism = LDAP_SASL_X511_STRONG;
485 } else {
486 method = LDAP_AUTH_SIMPLE;
489 #endif /* KERBEROS */
490 getaline( dn, sizeof(dn), stdin, "dn? " );
491 strcat( dn, dnsuffix );
493 if ( dn[0] != '\0' )
494 getaline( passwd, sizeof(passwd), stdin,
495 "password? " );
496 else
497 passwd[0] = '\0';
499 if (method == LDAP_AUTH_SIMPLE) {
500 if ( ldap_bind_s( ld, dn, passwd, method ) !=
501 LDAP_SUCCESS ) {
502 fprintf( stderr, "ldap_bind_s failed\n" );
503 ldap_perror( ld, "ldap_bind_s" );
504 } else {
505 printf( "Bind successful\n" );
506 bound = 1;
508 } else {
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" );
516 } else {
517 printf ( "Bind successful\n");
518 bound = 1;
520 } else {
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" );
527 break;
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){
536 bv.bv_val = value;
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) );
540 else
541 printf( "Compare initiated with id %d\n", id );
542 } else {
543 if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
544 ldap_perror( ld, "ldap_compare" );
545 else
546 printf( "Compare initiated with id %d\n", id );
548 break;
550 case 'd': /* turn on debugging */
551 #ifdef LDAP_DEBUG
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;
557 #else
558 printf( "Compile with -DLDAP_DEBUG for debugging\n" );
559 #endif
560 break;
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] );
568 break;
570 case 'g': /* set next msgid */
571 getaline( line, sizeof(line), stdin, "msgid? " );
572 ld->ld_msgid = atoi( line );
573 break;
575 case 'v': /* set version number */
576 getaline( line, sizeof(line), stdin, "version? " );
577 theInt = atoi(line);
578 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &theInt);
579 break;
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? " ))
588 == NULL )
589 break;
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) );
593 else
594 printf( "Modify initiated with id %d\n", id );
596 else {
597 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
598 ldap_perror( ld, "ldap_modify" );
599 else
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)?");
607 delrdn = atoi(line);
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));
612 else
613 printf( "Modrdn initiated with id %d\n", id );
615 else {
616 if ( (id = ldap_modrdn( ld, dn, rdn, delrdn )) == -1 )
617 ldap_perror( ld, "ldap_modrdn" );
618 else
619 printf( "Modrdn initiated with id %d\n", id );
621 } else {
622 printf( "Possibilities: [modi]fy, [modr]dn\n" );
624 break;
626 case 'q': /* quit */
627 #ifdef CLDAP
628 if ( cldapflg )
629 cldap_close( ld );
630 #endif /* CLDAP */
631 if ( !cldapflg )
632 ldap_unbind( ld );
633 exit( 0 );
634 break;
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' )
642 id = -1;
643 else
644 id = atoi( line );
645 getaline( line, sizeof(line), stdin,
646 "all (0=>any, 1=>all)? " );
647 if ( line[0] == '\0' )
648 all = 1;
649 else
650 all = atoi( line );
652 if (( msgtype = ldap_result( ld, id, all,
653 resultusetimelimit ? &timelimit : &timeout, &res )) < 1 ) {
654 ldap_perror( ld, "ldap_result" );
655 break;
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;
662 res = NULLMSG;
663 break;
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) );
671 else
672 printf( "Remove initiated with id %d\n", id );
673 } else {
674 if ( (id = ldap_delete( ld, dn )) == -1 )
675 ldap_perror( ld, "ldap_delete" );
676 else
677 printf( "Remove initiated with id %d\n", id );
679 break;
681 default:
682 printf( "Possibilities: [rem]ove, [res]ult\n" );
683 break;
685 break;
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 );
700 if ( cldapflg ) {
701 #ifdef CLDAP
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" );
707 } else {
708 printf( "\nresult: msgid %d\n",
709 res->lm_msgid );
710 handle_result( ld, res );
711 res = NULLMSG;
713 #endif /* CLDAP */
714 } else {
715 theInt = 0;
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)? " );
720 if (line[0] == '\0')
721 theInt = 1;
722 else
723 theInt = atoi( line );
725 if (theInt){
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));
734 } else {
735 printf( "Search initiated with id %d\n", id );
737 } else {
738 if (( id = ldap_search( ld, dn, scope, filter,
739 types, attrsonly )) == -1 ) {
740 ldap_perror( ld, "ldap_search" );
741 } else {
742 printf( "Search initiated with id %d\n", id );
746 free_list( types );
747 break;
749 case 't': /* set timeout value */
750 getaline( line, sizeof(line), stdin, "timeout? " );
751 timeout.tv_sec = atoi( line );
752 break;
754 case 'U': /* set ufn search prefix */
755 getaline( line, sizeof(line), stdin, "ufn prefix? " );
756 ldap_ufn_setprefix( ld, line );
757 break;
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,
770 &timeout );
771 } else {
772 id = ldap_ufn_search_s( ld, dn, types,
773 attrsonly, &res );
775 if ( res == NULL )
776 ldap_perror( ld, "ldap_ufn_search" );
777 else {
778 printf( "\nresult: err %d\n", id );
779 handle_result( ld, res );
780 res = NULLMSG;
782 free_list( types );
783 break;
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 ))
791 == -1 ) {
792 ldap_perror( ld, "ldap_url_search" );
793 } else {
794 printf( "URL search initiated with id %d\n", id );
796 break;
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 );
802 } else {
803 printf( "\t host: " );
804 if ( ludp->lud_host == NULL ) {
805 printf( "DEFAULT\n" );
806 } else {
807 printf( "<%s>\n", ludp->lud_host );
809 printf( "\t port: " );
810 if ( ludp->lud_port == 0 ) {
811 printf( "DEFAULT\n" );
812 } else {
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 ) {
818 printf( " ALL" );
819 } else {
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 );
838 break;
840 case 'n': /* set dn suffix, for convenience */
841 getaline( line, sizeof(line), stdin, "DN suffix? " );
842 strcpy( dnsuffix, line );
843 break;
845 case 'e': /* enable cache */
846 #ifdef NO_CACHE
847 printf( NOCACHEERRMSG );
848 #else /* NO_CACHE */
849 getaline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
850 i = atoi( line );
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" );
854 } else {
855 printf( "ldap_enable_cache failed\n" );
857 #endif /* NO_CACHE */
858 break;
860 case 'x': /* uncache entry */
861 #ifdef NO_CACHE
862 printf( NOCACHEERRMSG );
863 #else /* NO_CACHE */
864 getaline( line, sizeof(line), stdin, "DN? " );
865 ldap_uncache_entry( ld, line );
866 #endif /* NO_CACHE */
867 break;
869 case 'X': /* uncache request */
870 #ifdef NO_CACHE
871 printf( NOCACHEERRMSG );
872 #else /* NO_CACHE */
873 getaline( line, sizeof(line), stdin, "request msgid? " );
874 ldap_uncache_request( ld, atoi( line ));
875 #endif /* NO_CACHE */
876 break;
878 case 'o': /* set ldap options */
879 getaline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
880 theInt = atoi(line);
881 ldap_set_option(ld, LDAP_OPT_DEREF, &theInt );
882 getaline( line, sizeof(line), stdin, "timelimit?" );
883 theInt = atoi(line);
884 ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &theInt);
885 getaline( line, sizeof(line), stdin, "sizelimit?" );
886 theInt = atoi(line);
887 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &theInt);
889 ld->ld_options = 0;
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;
896 } else {
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,
903 ldap_8859_to_t61,
904 ldap_t61_to_8859 );
906 #endif /* LDAP_CHARSET_8859 */
908 #endif /* STR_TRANSLATION */
910 #ifdef LDAP_DNS
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 );
927 } else {
928 theInt = LDAP_OPT_OFF;
930 ldap_set_option(ld, LDAP_OPT_REFERRALS, &theInt);
931 break;
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)?");
936 theInt = atoi(line);
937 switch (theInt){
938 case 0:
939 ldap_set_option(ld, LDAP_OPT_SERVER_CONTROLS, NULL);
940 break;
941 case 1:
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);
946 break;
947 case 2:
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);
956 break;
957 default:
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);
964 break;
966 break;
968 case 'O': /* set cache options */
969 #ifdef NO_CACHE
970 printf( NOCACHEERRMSG );
971 #else /* NO_CACHE */
972 getaline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
973 switch( atoi( line )) {
974 case 0:
975 ldap_set_cache_options( ld, 0 );
976 break;
977 case 1:
978 ldap_set_cache_options( ld,
979 LDAP_CACHE_OPT_CACHENOERRS );
980 break;
981 case 2:
982 ldap_set_cache_options( ld,
983 LDAP_CACHE_OPT_CACHEALLERRS );
984 break;
985 default:
986 printf( "not a valid cache option\n" );
988 #endif /* NO_CACHE */
989 break;
991 case '?': /* help */
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" );
1002 break;
1004 default:
1005 printf( "Invalid command. Type ? for help.\n" );
1006 break;
1009 (void) memset( line, '\0', sizeof(line) );
1012 return( 0 );
1015 static void
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" );
1022 break;
1024 case LDAP_RES_SEARCH_RESULT:
1025 printf( "Search result\n" );
1026 print_ldap_result( ld, lm, "search" );
1027 break;
1029 case LDAP_RES_SEARCH_REFERENCE:
1030 printf( "Search reference\n" );
1031 print_search_entry( ld, lm );
1032 break;
1034 case LDAP_RES_SEARCH_ENTRY:
1035 printf( "Search entry\n" );
1036 print_search_entry( ld, lm );
1037 break;
1039 case LDAP_RES_ADD:
1040 printf( "Add result\n" );
1041 print_ldap_result( ld, lm, "add" );
1042 break;
1044 case LDAP_RES_DELETE:
1045 printf( "Delete result\n" );
1046 print_ldap_result( ld, lm, "delete" );
1047 break;
1049 case LDAP_RES_MODIFY:
1050 printf( "Modify result\n" );
1051 print_ldap_result( ld, lm, "modify" );
1052 break;
1054 case LDAP_RES_MODRDN:
1055 printf( "ModRDN result\n" );
1056 print_ldap_result( ld, lm, "modrdn" );
1057 break;
1059 case LDAP_RES_BIND:
1060 printf( "Bind result\n" );
1061 print_ldap_result( ld, lm, "bind" );
1062 break;
1064 default:
1065 printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
1066 print_ldap_result( ld, lm, "unknown" );
1070 static void
1071 print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
1073 int rc, i;
1074 int errCode;
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));
1080 return;
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);
1092 free(errMsg);
1093 if (NAME_ERROR(errCode) && matched && *matched){
1094 fprintf(stderr, "\tMatched DN: %s\n", matched);
1095 free(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");
1102 return;
1105 static void
1106 print_search_entry( LDAP *ld, LDAPMessage *res )
1108 BerElement *ber;
1109 char *a, *dn, *ufn;
1110 struct berval **vals;
1111 int i;
1112 LDAPMessage *e;
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 )
1117 break;
1119 dn = ldap_get_dn( ld, e );
1120 printf( "\tDN: %s\n", dn );
1122 ufn = ldap_dn2ufn( dn );
1123 printf( "\tUFN: %s\n", ufn );
1124 free( dn );
1125 free( ufn );
1127 if ( e->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ){
1128 char **urls = ldap_get_reference_urls(ld, e);
1129 if (urls == NULL){
1130 printf("\t\tError with references: %s\n", ldap_err2string(ld->ld_errno));
1131 } else {
1132 for (i=0;urls[i] != NULL;i++)
1133 printf("\t\tURL: %s\n", urls[i]);
1135 } else {
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 ))
1140 == NULL ) {
1141 printf( "\t\t\t(no values)\n" );
1142 } else {
1143 for ( i = 0; vals[i] != NULL; i++ ) {
1144 int j, nonascii;
1146 nonascii = 0;
1147 for ( j = 0; j < vals[i]->bv_len; j++ )
1148 if ( !isascii( vals[i]->bv_val[j] ) ) {
1149 nonascii = 1;
1150 break;
1153 if ( nonascii ) {
1154 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
1155 #ifdef BPRINT_NONASCII
1156 lber_bprint( vals[i]->bv_val,
1157 vals[i]->bv_len );
1158 #endif /* BPRINT_NONASCII */
1159 continue;
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" );