]> git.sur5r.net Git - openldap/blob - libraries/libldap/test.c
Fix large SASL reads. Use EAGAIN instead of EWOULDBLOCK (was right the
[openldap] / libraries / libldap / test.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17 #include <ac/unistd.h>
18
19 #include <sys/stat.h>
20
21 #ifdef HAVE_SYS_FILE_H
22 #include <sys/file.h>
23 #endif
24 #ifdef HAVE_IO_H
25 #include <io.h>
26 #endif
27
28 #include <fcntl.h>
29
30 /* including the "internal" defs is legit and nec. since this test routine has 
31  * a-priori knowledge of libldap internal workings.
32  * hodges@stanford.edu 5-Feb-96
33  */
34 #include "ldap-int.h"
35
36 /* local functions */
37 #ifndef HAVE_GETLINE
38 static char *getline LDAP_P(( char *line, int len, FILE *fp, const char *prompt ));
39 #endif
40 static char **get_list LDAP_P(( const char *prompt ));
41 static int file_read LDAP_P(( const char *path, struct berval *bv ));
42 static LDAPMod **get_modlist LDAP_P(( const char *prompt1,
43         const char *prompt2, const char *prompt3 ));
44 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
45 static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm,
46         const char *s ));
47 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
48 static void free_list LDAP_P(( char **list ));
49
50 #define NOCACHEERRMSG   "don't compile with -DLDAP_NOCACHE if you desire local caching"
51
52 static char *dnsuffix;
53
54 #ifndef HAVE_GETLINE
55 static char *
56 getline( char *line, int len, FILE *fp, const char *prompt )
57 {
58         printf(prompt);
59
60         if ( fgets( line, len, fp ) == NULL )
61                 return( NULL );
62
63         line[ strlen( line ) - 1 ] = '\0';
64
65         return( line );
66 }
67 #endif
68
69 static char **
70 get_list( const char *prompt )
71 {
72         static char     buf[256];
73         int             num;
74         char            **result;
75
76         num = 0;
77         result = (char **) 0;
78         while ( 1 ) {
79                 getline( buf, sizeof(buf), stdin, prompt );
80
81                 if ( *buf == '\0' )
82                         break;
83
84                 if ( result == (char **) 0 )
85                         result = (char **) malloc( sizeof(char *) );
86                 else
87                         result = (char **) realloc( result,
88                             sizeof(char *) * (num + 1) );
89
90                 result[num++] = (char *) strdup( buf );
91         }
92         if ( result == (char **) 0 )
93                 return( NULL );
94         result = (char **) realloc( result, sizeof(char *) * (num + 1) );
95         result[num] = NULL;
96
97         return( result );
98 }
99
100
101 static void
102 free_list( char **list )
103 {
104         int     i;
105
106         if ( list != NULL ) {
107                 for ( i = 0; list[ i ] != NULL; ++i ) {
108                         free( list[ i ] );
109                 }
110                 free( (char *)list );
111         }
112 }
113
114
115 static int
116 file_read( const char *path, struct berval *bv )
117 {
118         FILE            *fp;
119         ber_slen_t      rlen;
120         int             eof;
121
122         if (( fp = fopen( path, "r" )) == NULL ) {
123                 perror( path );
124                 return( -1 );
125         }
126
127         if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
128                 perror( path );
129                 fclose( fp );
130                 return( -1 );
131         }
132
133         bv->bv_len = ftell( fp );
134
135         if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
136                 perror( "malloc" );
137                 fclose( fp );
138                 return( -1 );
139         }
140
141         if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
142                 perror( path );
143                 fclose( fp );
144                 return( -1 );
145         }
146
147         rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
148         eof = feof( fp );
149         fclose( fp );
150
151         if ( (ber_len_t) rlen != bv->bv_len ) {
152                 perror( path );
153                 free( bv->bv_val );
154                 return( -1 );
155         }
156
157         return( bv->bv_len );
158 }
159
160
161 static LDAPMod **
162 get_modlist(
163         const char *prompt1,
164         const char *prompt2,
165         const char *prompt3 )
166 {
167         static char     buf[256];
168         int             num;
169         LDAPMod         tmp;
170         LDAPMod         **result;
171         struct berval   **bvals;
172
173         num = 0;
174         result = NULL;
175         while ( 1 ) {
176                 if ( prompt1 ) {
177                         getline( buf, sizeof(buf), stdin, prompt1 );
178                         tmp.mod_op = atoi( buf );
179
180                         if ( tmp.mod_op == -1 || buf[0] == '\0' )
181                                 break;
182                 }
183
184                 getline( buf, sizeof(buf), stdin, prompt2 );
185                 if ( buf[0] == '\0' )
186                         break;
187                 tmp.mod_type = strdup( buf );
188
189                 tmp.mod_values = get_list( prompt3 );
190
191                 if ( tmp.mod_values != NULL ) {
192                         int     i;
193
194                         for ( i = 0; tmp.mod_values[i] != NULL; ++i )
195                                 ;
196                         bvals = (struct berval **)calloc( i + 1,
197                             sizeof( struct berval *));
198                         for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
199                                 bvals[i] = (struct berval *)malloc(
200                                     sizeof( struct berval ));
201                                 if ( strncmp( tmp.mod_values[i], "{FILE}",
202                                     6 ) == 0 ) {
203                                         if ( file_read( tmp.mod_values[i] + 6,
204                                             bvals[i] ) < 0 ) {
205                                                 return( NULL );
206                                         }
207                                 } else {
208                                         bvals[i]->bv_val = tmp.mod_values[i];
209                                         bvals[i]->bv_len =
210                                             strlen( tmp.mod_values[i] );
211                                 }
212                         }
213                         tmp.mod_bvalues = bvals;
214                         tmp.mod_op |= LDAP_MOD_BVALUES;
215                 }
216
217                 if ( result == NULL )
218                         result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
219                 else
220                         result = (LDAPMod **) realloc( result,
221                             sizeof(LDAPMod *) * (num + 1) );
222
223                 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
224                 *(result[num]) = tmp;   /* struct copy */
225                 num++;
226         }
227         if ( result == NULL )
228                 return( NULL );
229         result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
230         result[num] = NULL;
231
232         return( result );
233 }
234
235
236 static int
237 bind_prompt( LDAP *ld,
238         LDAP_CONST char *url,
239         ber_tag_t request, ber_int_t msgid,
240         void *params )
241 {
242         static char     dn[256], passwd[256];
243         int     authmethod;
244
245         printf("rebind for request=%ld msgid=%ld url=%s\n",
246                 request, (long) msgid, url );
247
248 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
249                 getline( dn, sizeof(dn), stdin,
250                     "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
251         if (( authmethod = atoi( dn )) == 3 ) {
252                 authmethod = LDAP_AUTH_KRBV4;
253                 } else {
254                 authmethod |= 0x80;
255                 }
256 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
257         authmethod = LDAP_AUTH_SIMPLE;
258 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
259
260                 getline( dn, sizeof(dn), stdin, "re-bind dn? " );
261                 strcat( dn, dnsuffix );
262
263         if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
264                         getline( passwd, sizeof(passwd), stdin,
265                             "re-bind password? " );
266                 } else {
267                         passwd[0] = '\0';
268                 }
269
270         return ldap_bind_s( ld, dn, passwd, authmethod);
271 }
272
273
274 int
275 main( int argc, char **argv )
276 {
277         LDAP            *ld = NULL;
278         int             i, c, port, errflg, method, id, msgtype;
279         char            line[256], command1, command2, command3;
280         char            passwd[64], dn[256], rdn[64], attr[64], value[256];
281         char            filter[256], *host, **types;
282         char            **exdn;
283         char            *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
284         int             bound, all, scope, attrsonly;
285         LDAPMessage     *res;
286         LDAPMod         **mods, **attrs;
287         struct timeval  timeout;
288         char            *copyfname = NULL;
289         int             copyoptions = 0;
290         LDAPURLDesc     *ludp;
291
292         host = NULL;
293         port = LDAP_PORT;
294         dnsuffix = "";
295         errflg = 0;
296
297         while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
298                 switch( c ) {
299                 case 'd':
300 #ifdef LDAP_DEBUG
301                         ldap_debug = atoi( optarg );
302 #ifdef LBER_DEBUG
303                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
304                                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
305                         }
306 #endif
307 #else
308                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
309 #endif
310                         break;
311
312                 case 'h':
313                         host = optarg;
314                         break;
315
316                 case 's':
317                         dnsuffix = optarg;
318                         break;
319
320                 case 'p':
321                         port = atoi( optarg );
322                         break;
323
324                 case 't':       /* copy ber's to given file */
325                         copyfname = strdup( optarg );
326 /*                      copyoptions = LBER_TO_FILE; */
327                         break;
328
329                 case 'T':       /* only output ber's to given file */
330                         copyfname = strdup( optarg );
331 /*                      copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
332                         break;
333
334                 default:
335                     ++errflg;
336                 }
337         }
338
339         if ( host == NULL && optind == argc - 1 ) {
340                 host = argv[ optind ];
341                 ++optind;
342         }
343
344         if ( errflg || optind < argc - 1 ) {
345                 fprintf( stderr, usage, argv[ 0 ] );
346                 exit( EXIT_FAILURE );
347         }
348         
349         printf( "ldap_init( %s, %d )\n",
350                 host == NULL ? "(null)" : host, port );
351
352         ld = ldap_init( host, port );
353
354         if ( ld == NULL ) {
355                 perror( "ldap_init" );
356                 exit( EXIT_FAILURE );
357         }
358
359         if ( copyfname != NULL ) {
360                 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
361                     0600 ))  == -1 ) {
362                         perror( copyfname );
363                         exit ( EXIT_FAILURE );
364                 }
365                 ld->ld_sb->sb_options = copyoptions;
366         }
367
368         bound = 0;
369         timeout.tv_sec = 0;
370         timeout.tv_usec = 0;
371
372         (void) memset( line, '\0', sizeof(line) );
373         while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
374                 command1 = line[0];
375                 command2 = line[1];
376                 command3 = line[2];
377
378                 switch ( command1 ) {
379                 case 'a':       /* add or abandon */
380                         switch ( command2 ) {
381                         case 'd':       /* add */
382                                 getline( dn, sizeof(dn), stdin, "dn? " );
383                                 strcat( dn, dnsuffix );
384                                 if ( (attrs = get_modlist( NULL, "attr? ",
385                                     "value? " )) == NULL )
386                                         break;
387                                 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
388                                         ldap_perror( ld, "ldap_add" );
389                                 else
390                                         printf( "Add initiated with id %d\n",
391                                             id );
392                                 break;
393
394                         case 'b':       /* abandon */
395                                 getline( line, sizeof(line), stdin, "msgid? " );
396                                 id = atoi( line );
397                                 if ( ldap_abandon( ld, id ) != 0 )
398                                         ldap_perror( ld, "ldap_abandon" );
399                                 else
400                                         printf( "Abandon successful\n" );
401                                 break;
402                         default:
403                                 printf( "Possibilities: [ad]d, [ab]ort\n" );
404                         }
405                         break;
406
407                 case 'b':       /* asynch bind */
408 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
409                         getline( line, sizeof(line), stdin,
410                             "method (0->simple, 1->krbv41, 2->krbv42)? " );
411                         method = atoi( line ) | 0x80;
412 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
413                         method = LDAP_AUTH_SIMPLE;
414 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
415                         getline( dn, sizeof(dn), stdin, "dn? " );
416                         strcat( dn, dnsuffix );
417
418                         if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
419                                 getline( passwd, sizeof(passwd), stdin,
420                                     "password? " );
421                         else
422                                 passwd[0] = '\0';
423
424                         if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
425                                 fprintf( stderr, "ldap_bind failed\n" );
426                                 ldap_perror( ld, "ldap_bind" );
427                         } else {
428                                 printf( "Bind initiated\n" );
429                                 bound = 1;
430                         }
431                         break;
432
433                 case 'B':       /* synch bind */
434 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
435                         getline( line, sizeof(line), stdin,
436                             "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
437                         method = atoi( line );
438                         if ( method == 3 )
439                                 method = LDAP_AUTH_KRBV4;
440                         else
441                                 method = method | 0x80;
442 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
443                         method = LDAP_AUTH_SIMPLE;
444 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
445                         getline( dn, sizeof(dn), stdin, "dn? " );
446                         strcat( dn, dnsuffix );
447
448                         if ( dn[0] != '\0' )
449                                 getline( passwd, sizeof(passwd), stdin,
450                                     "password? " );
451                         else
452                                 passwd[0] = '\0';
453
454                         if ( ldap_bind_s( ld, dn, passwd, method ) !=
455                             LDAP_SUCCESS ) {
456                                 fprintf( stderr, "ldap_bind_s failed\n" );
457                                 ldap_perror( ld, "ldap_bind_s" );
458                         } else {
459                                 printf( "Bind successful\n" );
460                                 bound = 1;
461                         }
462                         break;
463
464                 case 'c':       /* compare */
465                         getline( dn, sizeof(dn), stdin, "dn? " );
466                         strcat( dn, dnsuffix );
467                         getline( attr, sizeof(attr), stdin, "attr? " );
468                         getline( value, sizeof(value), stdin, "value? " );
469
470                         if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
471                                 ldap_perror( ld, "ldap_compare" );
472                         else
473                                 printf( "Compare initiated with id %d\n", id );
474                         break;
475
476                 case 'd':       /* turn on debugging */
477 #ifdef LDAP_DEBUG
478                         getline( line, sizeof(line), stdin, "debug level? " );
479                         ldap_debug = atoi( line );
480 #ifdef LBER_DEBUG
481                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
482                                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
483                         }
484 #endif
485 #else
486                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
487 #endif
488                         break;
489
490                 case 'E':       /* explode a dn */
491                         getline( line, sizeof(line), stdin, "dn? " );
492                         exdn = ldap_explode_dn( line, 0 );
493                         for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
494                                 printf( "\t%s\n", exdn[i] );
495                         }
496                         break;
497
498                 case 'g':       /* set next msgid */
499                         getline( line, sizeof(line), stdin, "msgid? " );
500                         ld->ld_msgid = atoi( line );
501                         break;
502
503                 case 'v':       /* set version number */
504                         getline( line, sizeof(line), stdin, "version? " );
505                         ld->ld_version = atoi( line );
506                         break;
507
508                 case 'm':       /* modify or modifyrdn */
509                         if ( strncmp( line, "modify", 4 ) == 0 ) {
510                                 getline( dn, sizeof(dn), stdin, "dn? " );
511                                 strcat( dn, dnsuffix );
512                                 if ( (mods = get_modlist(
513                                     "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
514                                     "attribute type? ", "attribute value? " ))
515                                     == NULL )
516                                         break;
517                                 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
518                                         ldap_perror( ld, "ldap_modify" );
519                                 else
520                                         printf( "Modify initiated with id %d\n",
521                                             id );
522                         } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
523                                 getline( dn, sizeof(dn), stdin, "dn? " );
524                                 strcat( dn, dnsuffix );
525                                 getline( rdn, sizeof(rdn), stdin, "newrdn? " );
526                                 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
527                                         ldap_perror( ld, "ldap_modrdn" );
528                                 else
529                                         printf( "Modrdn initiated with id %d\n",
530                                             id );
531                         } else {
532                                 printf( "Possibilities: [modi]fy, [modr]dn\n" );
533                         }
534                         break;
535
536                 case 'q':       /* quit */
537                         ldap_unbind( ld );
538                         exit( EXIT_SUCCESS );
539                         break;
540
541                 case 'r':       /* result or remove */
542                         switch ( command3 ) {
543                         case 's':       /* result */
544                                 getline( line, sizeof(line), stdin,
545                                     "msgid (-1=>any)? " );
546                                 if ( line[0] == '\0' )
547                                         id = -1;
548                                 else
549                                         id = atoi( line );
550                                 getline( line, sizeof(line), stdin,
551                                     "all (0=>any, 1=>all)? " );
552                                 if ( line[0] == '\0' )
553                                         all = 1;
554                                 else
555                                         all = atoi( line );
556                                 if (( msgtype = ldap_result( ld, id, all,
557                                     &timeout, &res )) < 1 ) {
558                                         ldap_perror( ld, "ldap_result" );
559                                         break;
560                                 }
561                                 printf( "\nresult: msgtype %d msgid %d\n",
562                                     msgtype, res->lm_msgid );
563                                 handle_result( ld, res );
564                                 res = NULL;
565                                 break;
566
567                         case 'm':       /* remove */
568                                 getline( dn, sizeof(dn), stdin, "dn? " );
569                                 strcat( dn, dnsuffix );
570                                 if ( (id = ldap_delete( ld, dn )) == -1 )
571                                         ldap_perror( ld, "ldap_delete" );
572                                 else
573                                         printf( "Remove initiated with id %d\n",
574                                             id );
575                                 break;
576
577                         default:
578                                 printf( "Possibilities: [rem]ove, [res]ult\n" );
579                                 break;
580                         }
581                         break;
582
583                 case 's':       /* search */
584                         getline( dn, sizeof(dn), stdin, "searchbase? " );
585                         strcat( dn, dnsuffix );
586                         getline( line, sizeof(line), stdin,
587                             "scope (0=Base, 1=One Level, 2=Subtree)? " );
588                         scope = atoi( line );
589                         getline( filter, sizeof(filter), stdin,
590                             "search filter (e.g. sn=jones)? " );
591                         types = get_list( "attrs to return? " );
592                         getline( line, sizeof(line), stdin,
593                             "attrsonly (0=attrs&values, 1=attrs only)? " );
594                         attrsonly = atoi( line );
595
596                             if (( id = ldap_search( ld, dn, scope, filter,
597                                     types, attrsonly  )) == -1 ) {
598                                 ldap_perror( ld, "ldap_search" );
599                             } else {
600                                 printf( "Search initiated with id %d\n", id );
601                             }
602                         free_list( types );
603                         break;
604
605                 case 't':       /* set timeout value */
606                         getline( line, sizeof(line), stdin, "timeout? " );
607                         timeout.tv_sec = atoi( line );
608                         break;
609
610                 case 'p':       /* parse LDAP URL */
611                         getline( line, sizeof(line), stdin, "LDAP URL? " );
612                         if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
613                             fprintf( stderr, "ldap_url_parse: error %d\n", i );
614                         } else {
615                             printf( "\t  host: " );
616                             if ( ludp->lud_host == NULL ) {
617                                 printf( "DEFAULT\n" );
618                             } else {
619                                 printf( "<%s>\n", ludp->lud_host );
620                             }
621                             printf( "\t  port: " );
622                             if ( ludp->lud_port == 0 ) {
623                                 printf( "DEFAULT\n" );
624                             } else {
625                                 printf( "%d\n", ludp->lud_port );
626                             }
627                             printf( "\t    dn: <%s>\n", ludp->lud_dn );
628                             printf( "\t attrs:" );
629                             if ( ludp->lud_attrs == NULL ) {
630                                 printf( " ALL" );
631                             } else {
632                                 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
633                                     printf( " <%s>", ludp->lud_attrs[ i ] );
634                                 }
635                             }
636                             printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
637                                 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
638                                 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
639                             printf( "\tfilter: <%s>\n", ludp->lud_filter );
640                             ldap_free_urldesc( ludp );
641                         }
642                             break;
643
644                 case 'n':       /* set dn suffix, for convenience */
645                         getline( line, sizeof(line), stdin, "DN suffix? " );
646                         strcpy( dnsuffix, line );
647                         break;
648
649                 case 'e':       /* enable cache */
650 #ifdef LDAP_NOCACHE
651                         printf( NOCACHEERRMSG );
652 #else /* LDAP_NOCACHE */
653                         getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
654                         i = atoi( line );
655                         getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
656                         if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
657                                 printf( "local cache is on\n" ); 
658                         } else {
659                                 printf( "ldap_enable_cache failed\n" ); 
660                         }
661 #endif /* LDAP_NOCACHE */
662                         break;
663
664                 case 'x':       /* uncache entry */
665 #ifdef LDAP_NOCACHE
666                         printf( NOCACHEERRMSG );
667 #else /* LDAP_NOCACHE */
668                         getline( line, sizeof(line), stdin, "DN? " );
669                         ldap_uncache_entry( ld, line );
670 #endif /* LDAP_NOCACHE */
671                         break;
672
673                 case 'X':       /* uncache request */
674 #ifdef LDAP_NOCACHE
675                         printf( NOCACHEERRMSG );
676 #else /* LDAP_NOCACHE */
677                         getline( line, sizeof(line), stdin, "request msgid? " );
678                         ldap_uncache_request( ld, atoi( line ));
679 #endif /* LDAP_NOCACHE */
680                         break;
681
682                 case 'o':       /* set ldap options */
683                         getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
684                         ld->ld_deref = atoi( line );
685                         getline( line, sizeof(line), stdin, "timelimit?" );
686                         ld->ld_timelimit = atoi( line );
687                         getline( line, sizeof(line), stdin, "sizelimit?" );
688                         ld->ld_sizelimit = atoi( line );
689
690                         LDAP_BOOL_ZERO(&ld->ld_options);
691
692                         getline( line, sizeof(line), stdin,
693                                 "Recognize and chase referrals (0=no, 1=yes)?" );
694                         if ( atoi( line ) != 0 ) {
695                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
696                                 getline( line, sizeof(line), stdin,
697                                         "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
698                                 if ( atoi( line ) != 0 ) {
699                                         ldap_set_rebind_proc( ld, bind_prompt, NULL );
700                                 }
701                         }
702                         break;
703
704                 case 'O':       /* set cache options */
705 #ifdef LDAP_NOCACHE
706                         printf( NOCACHEERRMSG );
707 #else /* LDAP_NOCACHE */
708                         getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
709                         switch( atoi( line )) {
710                         case 0:
711                                 ldap_set_cache_options( ld, 0 );
712                                 break;
713                         case 1:
714                                 ldap_set_cache_options( ld,
715                                         LDAP_CACHE_OPT_CACHENOERRS );
716                                 break;
717                         case 2:
718                                 ldap_set_cache_options( ld,
719                                         LDAP_CACHE_OPT_CACHEALLERRS );
720                                 break;
721                         default:
722                                 printf( "not a valid cache option\n" );
723                         }
724 #endif /* LDAP_NOCACHE */
725                         break;
726
727                 case '?':       /* help */
728     printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
729     printf( "          [B]ind async  [c]ompare\n" );
730     printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
731     printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
732     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
733     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
734     printf( "          [?]help       [o]ptions         [O]cache options\n" );
735     printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
736     printf( "          [x]uncache entry  [X]uncache request\n" );
737                         break;
738
739                 default:
740                         printf( "Invalid command.  Type ? for help.\n" );
741                         break;
742                 }
743
744                 (void) memset( line, '\0', sizeof(line) );
745         }
746
747         return( 0 );
748 }
749
750 static void
751 handle_result( LDAP *ld, LDAPMessage *lm )
752 {
753         switch ( lm->lm_msgtype ) {
754         case LDAP_RES_COMPARE:
755                 printf( "Compare result\n" );
756                 print_ldap_result( ld, lm, "compare" );
757                 break;
758
759         case LDAP_RES_SEARCH_RESULT:
760                 printf( "Search result\n" );
761                 print_ldap_result( ld, lm, "search" );
762                 break;
763
764         case LDAP_RES_SEARCH_ENTRY:
765                 printf( "Search entry\n" );
766                 print_search_entry( ld, lm );
767                 break;
768
769         case LDAP_RES_ADD:
770                 printf( "Add result\n" );
771                 print_ldap_result( ld, lm, "add" );
772                 break;
773
774         case LDAP_RES_DELETE:
775                 printf( "Delete result\n" );
776                 print_ldap_result( ld, lm, "delete" );
777                 break;
778
779         case LDAP_RES_MODRDN:
780                 printf( "ModRDN result\n" );
781                 print_ldap_result( ld, lm, "modrdn" );
782                 break;
783
784         case LDAP_RES_BIND:
785                 printf( "Bind result\n" );
786                 print_ldap_result( ld, lm, "bind" );
787                 break;
788
789         default:
790                 printf( "Unknown result type 0x%lx\n",
791                         (unsigned long) lm->lm_msgtype );
792                 print_ldap_result( ld, lm, "unknown" );
793         }
794 }
795
796 static void
797 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
798 {
799         ldap_result2error( ld, lm, 1 );
800         ldap_perror( ld, s );
801 /*
802         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
803                 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
804         if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
805                 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
806 */
807 }
808
809 static void
810 print_search_entry( LDAP *ld, LDAPMessage *res )
811 {
812         LDAPMessage     *e;
813
814         for ( e = ldap_first_entry( ld, res ); e != NULL;
815             e = ldap_next_entry( ld, e ) )
816         {
817                 BerElement      *ber = NULL;
818                 char *a, *dn, *ufn;
819
820                 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
821                         break;
822
823                 dn = ldap_get_dn( ld, e );
824                 printf( "\tDN: %s\n", dn );
825
826                 ufn = ldap_dn2ufn( dn );
827                 printf( "\tUFN: %s\n", ufn );
828
829                 free( dn );
830                 free( ufn );
831
832                 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
833                     a = ldap_next_attribute( ld, e, ber ) )
834                 {
835                         struct berval   **vals;
836
837                         printf( "\t\tATTR: %s\n", a );
838                         if ( (vals = ldap_get_values_len( ld, e, a ))
839                             == NULL ) {
840                                 printf( "\t\t\t(no values)\n" );
841                         } else {
842                                 int i;
843                                 for ( i = 0; vals[i] != NULL; i++ ) {
844                                         int     j, nonascii;
845
846                                         nonascii = 0;
847                                         for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
848                                                 if ( !isascii( vals[i]->bv_val[j] ) ) {
849                                                         nonascii = 1;
850                                                         break;
851                                                 }
852
853                                         if ( nonascii ) {
854                                                 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
855 #ifdef BPRINT_NONASCII
856                                                 ber_bprint( vals[i]->bv_val,
857                                                     vals[i]->bv_len );
858 #endif /* BPRINT_NONASCII */
859                                                 continue;
860                                         }
861                                         printf( "\t\t\tlength (%ld) %s\n",
862                                             vals[i]->bv_len, vals[i]->bv_val );
863                                 }
864                                 ber_bvecfree( vals );
865                         }
866                 }
867
868                 if(ber != NULL) {
869                         ber_free( ber, 0 );
870                 }
871         }
872
873         if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
874             || res->lm_chain != NULL )
875                 print_ldap_result( ld, res, "search" );
876 }