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