]> git.sur5r.net Git - openldap/blob - libraries/libldap/test.c
Use getpassphrase() instead of getpass() if available.
[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, char **dnp, char **passwdp, int *authmethodp,
233         int freeit )
234 {
235         static char     dn[256], passwd[256];
236
237         if ( !freeit ) {
238 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
239                 getline( dn, sizeof(dn), stdin,
240                     "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
241                 if (( *authmethodp = atoi( dn )) == 3 ) {
242                         *authmethodp = LDAP_AUTH_KRBV4;
243                 } else {
244                         *authmethodp |= 0x80;
245                 }
246 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
247                 *authmethodp = LDAP_AUTH_SIMPLE;
248 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
249
250                 getline( dn, sizeof(dn), stdin, "re-bind dn? " );
251                 strcat( dn, dnsuffix );
252                 *dnp = dn;
253
254                 if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
255                         getline( passwd, sizeof(passwd), stdin,
256                             "re-bind password? " );
257                 } else {
258                         passwd[0] = '\0';
259                 }
260                 *passwdp = passwd;
261         }
262
263         return( LDAP_SUCCESS );
264 }
265
266
267 int
268 main( int argc, char **argv )
269 {
270         LDAP            *ld = NULL;
271         int             i, c, port, cldapflg, errflg, method, id, msgtype;
272         char            line[256], command1, command2, command3;
273         char            passwd[64], dn[256], rdn[64], attr[64], value[256];
274         char            filter[256], *host, **types;
275         char            **exdn;
276         char            *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
277         int             bound, all, scope, attrsonly;
278         LDAPMessage     *res;
279         LDAPMod         **mods, **attrs;
280         struct timeval  timeout;
281         char            *copyfname = NULL;
282         int             copyoptions = 0;
283         LDAPURLDesc     *ludp;
284
285         host = NULL;
286         port = LDAP_PORT;
287         dnsuffix = "";
288         cldapflg = errflg = 0;
289
290         while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
291                 switch( c ) {
292                 case 'u':
293 #ifdef LDAP_CONNECTIONLESS
294                         cldapflg++;
295 #else /* LDAP_CONNECTIONLESS */
296                         printf( "Compile with -DLDAP_CONNECTIONLESS for UDP support\n" );
297 #endif /* LDAP_CONNECTIONLESS */
298                         break;
299
300                 case 'd':
301 #ifdef LDAP_DEBUG
302                         ldap_debug = atoi( optarg );
303 #ifdef LBER_DEBUG
304                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
305                                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
306                         }
307 #endif
308 #else
309                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
310 #endif
311                         break;
312
313                 case 'h':
314                         host = optarg;
315                         break;
316
317                 case 's':
318                         dnsuffix = optarg;
319                         break;
320
321                 case 'p':
322                         port = atoi( optarg );
323                         break;
324
325                 case 't':       /* copy ber's to given file */
326                         copyfname = strdup( optarg );
327                         copyoptions = LBER_TO_FILE;
328                         break;
329
330                 case 'T':       /* only output ber's to given file */
331                         copyfname = strdup( optarg );
332                         copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
333                         break;
334
335                 default:
336                     ++errflg;
337                 }
338         }
339
340         if ( host == NULL && optind == argc - 1 ) {
341                 host = argv[ optind ];
342                 ++optind;
343         }
344
345         if ( errflg || optind < argc - 1 ) {
346                 fprintf( stderr, usage, argv[ 0 ] );
347                 exit( EXIT_FAILURE );
348         }
349         
350         printf( "%s( %s, %d )\n",
351                 cldapflg ? "cldap_open" : "ldap_init",
352                 host == NULL ? "(null)" : host, port );
353
354         if ( cldapflg ) {
355 #ifdef LDAP_CONNECTIONLESS
356                 ld = cldap_open( host, port );
357 #endif /* LDAP_CONNECTIONLESS */
358         } else {
359                 ld = ldap_init( host, port );
360         }
361
362         if ( ld == NULL ) {
363                 perror( cldapflg ? "cldap_open" : "ldap_init" );
364                 exit( EXIT_FAILURE );
365         }
366
367         if ( copyfname != NULL ) {
368                 if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
369                     0600 ))  == -1 ) {
370                         perror( copyfname );
371                         exit ( EXIT_FAILURE );
372                 }
373                 ld->ld_sb.sb_options = copyoptions;
374         }
375
376         bound = 0;
377         timeout.tv_sec = 0;
378         timeout.tv_usec = 0;
379
380         (void) memset( line, '\0', sizeof(line) );
381         while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
382                 command1 = line[0];
383                 command2 = line[1];
384                 command3 = line[2];
385
386                 switch ( command1 ) {
387                 case 'a':       /* add or abandon */
388                         switch ( command2 ) {
389                         case 'd':       /* add */
390                                 getline( dn, sizeof(dn), stdin, "dn? " );
391                                 strcat( dn, dnsuffix );
392                                 if ( (attrs = get_modlist( NULL, "attr? ",
393                                     "value? " )) == NULL )
394                                         break;
395                                 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
396                                         ldap_perror( ld, "ldap_add" );
397                                 else
398                                         printf( "Add initiated with id %d\n",
399                                             id );
400                                 break;
401
402                         case 'b':       /* abandon */
403                                 getline( line, sizeof(line), stdin, "msgid? " );
404                                 id = atoi( line );
405                                 if ( ldap_abandon( ld, id ) != 0 )
406                                         ldap_perror( ld, "ldap_abandon" );
407                                 else
408                                         printf( "Abandon successful\n" );
409                                 break;
410                         default:
411                                 printf( "Possibilities: [ad]d, [ab]ort\n" );
412                         }
413                         break;
414
415                 case 'b':       /* asynch bind */
416 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
417                         getline( line, sizeof(line), stdin,
418                             "method (0->simple, 1->krbv41, 2->krbv42)? " );
419                         method = atoi( line ) | 0x80;
420 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
421                         method = LDAP_AUTH_SIMPLE;
422 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
423                         getline( dn, sizeof(dn), stdin, "dn? " );
424                         strcat( dn, dnsuffix );
425
426                         if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
427                                 getline( passwd, sizeof(passwd), stdin,
428                                     "password? " );
429                         else
430                                 passwd[0] = '\0';
431
432                         if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
433                                 fprintf( stderr, "ldap_bind failed\n" );
434                                 ldap_perror( ld, "ldap_bind" );
435                         } else {
436                                 printf( "Bind initiated\n" );
437                                 bound = 1;
438                         }
439                         break;
440
441                 case 'B':       /* synch bind */
442 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
443                         getline( line, sizeof(line), stdin,
444                             "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
445                         method = atoi( line );
446                         if ( method == 3 )
447                                 method = LDAP_AUTH_KRBV4;
448                         else
449                                 method = method | 0x80;
450 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
451                         method = LDAP_AUTH_SIMPLE;
452 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
453                         getline( dn, sizeof(dn), stdin, "dn? " );
454                         strcat( dn, dnsuffix );
455
456                         if ( dn[0] != '\0' )
457                                 getline( passwd, sizeof(passwd), stdin,
458                                     "password? " );
459                         else
460                                 passwd[0] = '\0';
461
462                         if ( ldap_bind_s( ld, dn, passwd, method ) !=
463                             LDAP_SUCCESS ) {
464                                 fprintf( stderr, "ldap_bind_s failed\n" );
465                                 ldap_perror( ld, "ldap_bind_s" );
466                         } else {
467                                 printf( "Bind successful\n" );
468                                 bound = 1;
469                         }
470                         break;
471
472                 case 'c':       /* compare */
473                         getline( dn, sizeof(dn), stdin, "dn? " );
474                         strcat( dn, dnsuffix );
475                         getline( attr, sizeof(attr), stdin, "attr? " );
476                         getline( value, sizeof(value), stdin, "value? " );
477
478                         if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
479                                 ldap_perror( ld, "ldap_compare" );
480                         else
481                                 printf( "Compare initiated with id %d\n", id );
482                         break;
483
484                 case 'd':       /* turn on debugging */
485 #ifdef LDAP_DEBUG
486                         getline( line, sizeof(line), stdin, "debug level? " );
487                         ldap_debug = atoi( line );
488 #ifdef LBER_DEBUG
489                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
490                                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
491                         }
492 #endif
493 #else
494                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
495 #endif
496                         break;
497
498                 case 'E':       /* explode a dn */
499                         getline( line, sizeof(line), stdin, "dn? " );
500                         exdn = ldap_explode_dn( line, 0 );
501                         for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
502                                 printf( "\t%s\n", exdn[i] );
503                         }
504                         break;
505
506                 case 'g':       /* set next msgid */
507                         getline( line, sizeof(line), stdin, "msgid? " );
508                         ld->ld_msgid = atoi( line );
509                         break;
510
511                 case 'v':       /* set version number */
512                         getline( line, sizeof(line), stdin, "version? " );
513                         ld->ld_version = atoi( line );
514                         break;
515
516                 case 'm':       /* modify or modifyrdn */
517                         if ( strncmp( line, "modify", 4 ) == 0 ) {
518                                 getline( dn, sizeof(dn), stdin, "dn? " );
519                                 strcat( dn, dnsuffix );
520                                 if ( (mods = get_modlist(
521                                     "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
522                                     "attribute type? ", "attribute value? " ))
523                                     == NULL )
524                                         break;
525                                 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
526                                         ldap_perror( ld, "ldap_modify" );
527                                 else
528                                         printf( "Modify initiated with id %d\n",
529                                             id );
530                         } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
531                                 getline( dn, sizeof(dn), stdin, "dn? " );
532                                 strcat( dn, dnsuffix );
533                                 getline( rdn, sizeof(rdn), stdin, "newrdn? " );
534                                 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
535                                         ldap_perror( ld, "ldap_modrdn" );
536                                 else
537                                         printf( "Modrdn initiated with id %d\n",
538                                             id );
539                         } else {
540                                 printf( "Possibilities: [modi]fy, [modr]dn\n" );
541                         }
542                         break;
543
544                 case 'q':       /* quit */
545 #ifdef LDAP_CONNECTIONLESS
546                         if ( cldapflg )
547                                 cldap_close( ld );
548 #endif /* LDAP_CONNECTIONLESS */
549
550                         if ( !cldapflg ) {
551                                 ldap_unbind( ld );
552                         }
553                         exit( EXIT_SUCCESS );
554                         break;
555
556                 case 'r':       /* result or remove */
557                         switch ( command3 ) {
558                         case 's':       /* result */
559                                 getline( line, sizeof(line), stdin,
560                                     "msgid (-1=>any)? " );
561                                 if ( line[0] == '\0' )
562                                         id = -1;
563                                 else
564                                         id = atoi( line );
565                                 getline( line, sizeof(line), stdin,
566                                     "all (0=>any, 1=>all)? " );
567                                 if ( line[0] == '\0' )
568                                         all = 1;
569                                 else
570                                         all = atoi( line );
571                                 if (( msgtype = ldap_result( ld, id, all,
572                                     &timeout, &res )) < 1 ) {
573                                         ldap_perror( ld, "ldap_result" );
574                                         break;
575                                 }
576                                 printf( "\nresult: msgtype %d msgid %d\n",
577                                     msgtype, res->lm_msgid );
578                                 handle_result( ld, res );
579                                 res = NULL;
580                                 break;
581
582                         case 'm':       /* remove */
583                                 getline( dn, sizeof(dn), stdin, "dn? " );
584                                 strcat( dn, dnsuffix );
585                                 if ( (id = ldap_delete( ld, dn )) == -1 )
586                                         ldap_perror( ld, "ldap_delete" );
587                                 else
588                                         printf( "Remove initiated with id %d\n",
589                                             id );
590                                 break;
591
592                         default:
593                                 printf( "Possibilities: [rem]ove, [res]ult\n" );
594                                 break;
595                         }
596                         break;
597
598                 case 's':       /* search */
599                         getline( dn, sizeof(dn), stdin, "searchbase? " );
600                         strcat( dn, dnsuffix );
601                         getline( line, sizeof(line), stdin,
602                             "scope (0=Base, 1=One Level, 2=Subtree)? " );
603                         scope = atoi( line );
604                         getline( filter, sizeof(filter), stdin,
605                             "search filter (e.g. sn=jones)? " );
606                         types = get_list( "attrs to return? " );
607                         getline( line, sizeof(line), stdin,
608                             "attrsonly (0=attrs&values, 1=attrs only)? " );
609                         attrsonly = atoi( line );
610
611                         if ( cldapflg ) {
612 #ifdef LDAP_CONNECTIONLESS
613                             getline( line, sizeof(line), stdin,
614                                 "Requestor DN (for logging)? " );
615                             if ( cldap_search_s( ld, dn, scope, filter, types,
616                                     attrsonly, &res, line ) != 0 ) {
617                                 ldap_perror( ld, "cldap_search_s" );
618                             } else {
619                                 printf( "\nresult: msgid %d\n",
620                                     res->lm_msgid );
621                                 handle_result( ld, res );
622                                 res = NULL;
623                             }
624 #endif /* LDAP_CONNECTIONLESS */
625                         } else {
626                             if (( id = ldap_search( ld, dn, scope, filter,
627                                     types, attrsonly  )) == -1 ) {
628                                 ldap_perror( ld, "ldap_search" );
629                             } else {
630                                 printf( "Search initiated with id %d\n", id );
631                             }
632                         }
633                         free_list( types );
634                         break;
635
636                 case 't':       /* set timeout value */
637                         getline( line, sizeof(line), stdin, "timeout? " );
638                         timeout.tv_sec = atoi( line );
639                         break;
640
641                 case 'U':       /* set ufn search prefix */
642                         getline( line, sizeof(line), stdin, "ufn prefix? " );
643                         ldap_ufn_setprefix( ld, line );
644                         break;
645
646                 case 'u':       /* user friendly search w/optional timeout */
647                         getline( dn, sizeof(dn), stdin, "ufn? " );
648                         strcat( dn, dnsuffix );
649                         types = get_list( "attrs to return? " );
650                         getline( line, sizeof(line), stdin,
651                             "attrsonly (0=attrs&values, 1=attrs only)? " );
652                         attrsonly = atoi( line );
653
654                         if ( command2 == 't' ) {
655                                 id = ldap_ufn_search_c( ld, dn, types,
656                                     attrsonly, &res, ldap_ufn_timeout,
657                                     &timeout );
658                         } else {
659                                 id = ldap_ufn_search_s( ld, dn, types,
660                                     attrsonly, &res );
661                         }
662                         if ( res == NULL )
663                                 ldap_perror( ld, "ldap_ufn_search" );
664                         else {
665                                 printf( "\nresult: err %d\n", id );
666                                 handle_result( ld, res );
667                                 res = NULL;
668                         }
669                         free_list( types );
670                         break;
671
672                 case 'l':       /* URL search */
673                         getline( line, sizeof(line), stdin,
674                             "attrsonly (0=attrs&values, 1=attrs only)? " );
675                         attrsonly = atoi( line );
676                         getline( line, sizeof(line), stdin, "LDAP URL? " );
677                         if (( id = ldap_url_search( ld, line, attrsonly  ))
678                                 == -1 ) {
679                             ldap_perror( ld, "ldap_url_search" );
680                         } else {
681                             printf( "URL search initiated with id %d\n", id );
682                         }
683                         break;
684
685                 case 'p':       /* parse LDAP URL */
686                         getline( line, sizeof(line), stdin, "LDAP URL? " );
687                         if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
688                             fprintf( stderr, "ldap_url_parse: error %d\n", i );
689                         } else {
690                             printf( "\t  host: " );
691                             if ( ludp->lud_host == NULL ) {
692                                 printf( "DEFAULT\n" );
693                             } else {
694                                 printf( "<%s>\n", ludp->lud_host );
695                             }
696                             printf( "\t  port: " );
697                             if ( ludp->lud_port == 0 ) {
698                                 printf( "DEFAULT\n" );
699                             } else {
700                                 printf( "%d\n", ludp->lud_port );
701                             }
702                             printf( "\t    dn: <%s>\n", ludp->lud_dn );
703                             printf( "\t attrs:" );
704                             if ( ludp->lud_attrs == NULL ) {
705                                 printf( " ALL" );
706                             } else {
707                                 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
708                                     printf( " <%s>", ludp->lud_attrs[ i ] );
709                                 }
710                             }
711                             printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
712                                 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
713                                 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
714                             printf( "\tfilter: <%s>\n", ludp->lud_filter );
715                             ldap_free_urldesc( ludp );
716                         }
717                             break;
718
719                 case 'n':       /* set dn suffix, for convenience */
720                         getline( line, sizeof(line), stdin, "DN suffix? " );
721                         strcpy( dnsuffix, line );
722                         break;
723
724                 case 'e':       /* enable cache */
725 #ifdef LDAP_NOCACHE
726                         printf( NOCACHEERRMSG );
727 #else /* LDAP_NOCACHE */
728                         getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
729                         i = atoi( line );
730                         getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
731                         if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
732                                 printf( "local cache is on\n" ); 
733                         } else {
734                                 printf( "ldap_enable_cache failed\n" ); 
735                         }
736 #endif /* LDAP_NOCACHE */
737                         break;
738
739                 case 'x':       /* uncache entry */
740 #ifdef LDAP_NOCACHE
741                         printf( NOCACHEERRMSG );
742 #else /* LDAP_NOCACHE */
743                         getline( line, sizeof(line), stdin, "DN? " );
744                         ldap_uncache_entry( ld, line );
745 #endif /* LDAP_NOCACHE */
746                         break;
747
748                 case 'X':       /* uncache request */
749 #ifdef LDAP_NOCACHE
750                         printf( NOCACHEERRMSG );
751 #else /* LDAP_NOCACHE */
752                         getline( line, sizeof(line), stdin, "request msgid? " );
753                         ldap_uncache_request( ld, atoi( line ));
754 #endif /* LDAP_NOCACHE */
755                         break;
756
757                 case 'o':       /* set ldap options */
758                         getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
759                         ld->ld_deref = atoi( line );
760                         getline( line, sizeof(line), stdin, "timelimit?" );
761                         ld->ld_timelimit = atoi( line );
762                         getline( line, sizeof(line), stdin, "sizelimit?" );
763                         ld->ld_sizelimit = atoi( line );
764
765                         LDAP_BOOL_ZERO(&ld->ld_options);
766
767 #ifdef STR_TRANSLATION
768                         getline( line, sizeof(line), stdin,
769                                 "Automatic translation of T.61 strings (0=no, 1=yes)?" );
770                         if ( atoi( line ) == 0 ) {
771                                 ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
772                         } else {
773                                 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
774 #ifdef LDAP_CHARSET_8859
775                                 getline( line, sizeof(line), stdin,
776                                         "Translate to/from ISO-8859 (0=no, 1=yes?" );
777                                 if ( atoi( line ) != 0 ) {
778                                         ldap_set_string_translators( ld,
779                                             ldap_8859_to_t61,
780                                             ldap_t61_to_8859 );
781                                 }
782 #endif /* LDAP_CHARSET_8859 */
783                         }
784 #endif /* STR_TRANSLATION */
785
786 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
787                         getline( line, sizeof(line), stdin,
788                                 "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
789                         if ( atoi( line ) != 0 ) {
790                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS);
791                         }
792 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
793
794                         getline( line, sizeof(line), stdin,
795                                 "Recognize and chase referrals (0=no, 1=yes)?" );
796                         if ( atoi( line ) != 0 ) {
797                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
798                                 getline( line, sizeof(line), stdin,
799                                         "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
800                                 if ( atoi( line ) != 0 ) {
801                                         ldap_set_rebind_proc( ld, bind_prompt );
802                                 }
803                         }
804                         break;
805
806                 case 'O':       /* set cache options */
807 #ifdef LDAP_NOCACHE
808                         printf( NOCACHEERRMSG );
809 #else /* LDAP_NOCACHE */
810                         getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
811                         switch( atoi( line )) {
812                         case 0:
813                                 ldap_set_cache_options( ld, 0 );
814                                 break;
815                         case 1:
816                                 ldap_set_cache_options( ld,
817                                         LDAP_CACHE_OPT_CACHENOERRS );
818                                 break;
819                         case 2:
820                                 ldap_set_cache_options( ld,
821                                         LDAP_CACHE_OPT_CACHEALLERRS );
822                                 break;
823                         default:
824                                 printf( "not a valid cache option\n" );
825                         }
826 #endif /* LDAP_NOCACHE */
827                         break;
828
829                 case '?':       /* help */
830     printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
831     printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
832     printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
833     printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
834     printf( "          [u]fn search  [ut]fn search with timeout\n" );
835     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
836     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
837     printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\n" );
838     printf( "          [?]help       [o]ptions         [O]cache options\n" );
839     printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
840                         break;
841
842                 default:
843                         printf( "Invalid command.  Type ? for help.\n" );
844                         break;
845                 }
846
847                 (void) memset( line, '\0', sizeof(line) );
848         }
849
850         return( 0 );
851 }
852
853 static void
854 handle_result( LDAP *ld, LDAPMessage *lm )
855 {
856         switch ( lm->lm_msgtype ) {
857         case LDAP_RES_COMPARE:
858                 printf( "Compare result\n" );
859                 print_ldap_result( ld, lm, "compare" );
860                 break;
861
862         case LDAP_RES_SEARCH_RESULT:
863                 printf( "Search result\n" );
864                 print_ldap_result( ld, lm, "search" );
865                 break;
866
867         case LDAP_RES_SEARCH_ENTRY:
868                 printf( "Search entry\n" );
869                 print_search_entry( ld, lm );
870                 break;
871
872         case LDAP_RES_ADD:
873                 printf( "Add result\n" );
874                 print_ldap_result( ld, lm, "add" );
875                 break;
876
877         case LDAP_RES_DELETE:
878                 printf( "Delete result\n" );
879                 print_ldap_result( ld, lm, "delete" );
880                 break;
881
882         case LDAP_RES_MODRDN:
883                 printf( "ModRDN result\n" );
884                 print_ldap_result( ld, lm, "modrdn" );
885                 break;
886
887         case LDAP_RES_BIND:
888                 printf( "Bind result\n" );
889                 print_ldap_result( ld, lm, "bind" );
890                 break;
891
892         default:
893                 printf( "Unknown result type 0x%lx\n",
894                         (unsigned long) lm->lm_msgtype );
895                 print_ldap_result( ld, lm, "unknown" );
896         }
897 }
898
899 static void
900 print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
901 {
902         ldap_result2error( ld, lm, 1 );
903         ldap_perror( ld, s );
904 /*
905         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
906                 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
907         if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
908                 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
909 */
910 }
911
912 static void
913 print_search_entry( LDAP *ld, LDAPMessage *res )
914 {
915         LDAPMessage     *e;
916
917         for ( e = ldap_first_entry( ld, res ); e != NULL;
918             e = ldap_next_entry( ld, e ) )
919         {
920                 BerElement      *ber = NULL;
921                 char *a, *dn, *ufn;
922
923                 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
924                         break;
925
926                 dn = ldap_get_dn( ld, e );
927                 printf( "\tDN: %s\n", dn );
928
929                 ufn = ldap_dn2ufn( dn );
930                 printf( "\tUFN: %s\n", ufn );
931
932                 free( dn );
933                 free( ufn );
934
935                 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
936                     a = ldap_next_attribute( ld, e, ber ) )
937                 {
938                         struct berval   **vals;
939
940                         printf( "\t\tATTR: %s\n", a );
941                         if ( (vals = ldap_get_values_len( ld, e, a ))
942                             == NULL ) {
943                                 printf( "\t\t\t(no values)\n" );
944                         } else {
945                                 int i;
946                                 for ( i = 0; vals[i] != NULL; i++ ) {
947                                         int     j, nonascii;
948
949                                         nonascii = 0;
950                                         for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
951                                                 if ( !isascii( vals[i]->bv_val[j] ) ) {
952                                                         nonascii = 1;
953                                                         break;
954                                                 }
955
956                                         if ( nonascii ) {
957                                                 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
958 #ifdef BPRINT_NONASCII
959                                                 ber_bprint( vals[i]->bv_val,
960                                                     vals[i]->bv_len );
961 #endif /* BPRINT_NONASCII */
962                                                 continue;
963                                         }
964                                         printf( "\t\t\tlength (%ld) %s\n",
965                                             vals[i]->bv_len, vals[i]->bv_val );
966                                 }
967                                 ber_bvecfree( vals );
968                         }
969                 }
970
971                 if(ber != NULL) {
972                         ber_free( ber, 0 );
973                 }
974         }
975
976         if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
977             || res->lm_chain != NULL )
978                 print_ldap_result( ld, res, "search" );
979 }