]> git.sur5r.net Git - openldap/blob - libraries/libldap/test.c
7e3770f53826bf72be15fe360dd4fe62cb605490
[openldap] / libraries / libldap / test.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6 #include "portable.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <ac/ctype.h>
12 #include <ac/socket.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15 #include <ac/unistd.h>
16
17 #include <sys/stat.h>
18
19 #ifdef HAVE_SYS_FILE_H
20 #include <sys/file.h>
21 #endif
22 #ifdef HAVE_IO_H
23 #include <io.h>
24 #endif
25
26 #include <fcntl.h>
27
28 /* including the "internal" defs is legit and nec. since this test routine has 
29  * a-priori knowledge of libldap internal workings.
30  * hodges@stanford.edu 5-Feb-96
31  */
32 #include "ldap-int.h"
33
34 /* local functions */
35 #ifndef HAVE_GETLINE
36 static char *getline LDAP_P(( char *line, int len, FILE *fp, char *prompt ));
37 #endif
38 static char **get_list LDAP_P(( char *prompt ));
39 static int file_read LDAP_P(( char *path, struct berval *bv ));
40 static LDAPMod **get_modlist LDAP_P(( char *prompt1, char *prompt2, char *prompt3 ));
41 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
42 static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm, char *s ));
43 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
44 static void free_list LDAP_P(( char **list ));
45
46 #define NOCACHEERRMSG   "don't compile with -DLDAP_NOCACHE if you desire local caching"
47
48 static char *dnsuffix;
49
50 #ifndef HAVE_GETLINE
51 static char *
52 getline( char *line, int len, FILE *fp, char *prompt )
53 {
54         printf(prompt);
55
56         if ( fgets( line, len, fp ) == NULL )
57                 return( NULL );
58
59         line[ strlen( line ) - 1 ] = '\0';
60
61         return( line );
62 }
63 #endif
64
65 static char **
66 get_list( char *prompt )
67 {
68         static char     buf[256];
69         int             num;
70         char            **result;
71
72         num = 0;
73         result = (char **) 0;
74         while ( 1 ) {
75                 getline( buf, sizeof(buf), stdin, prompt );
76
77                 if ( *buf == '\0' )
78                         break;
79
80                 if ( result == (char **) 0 )
81                         result = (char **) malloc( sizeof(char *) );
82                 else
83                         result = (char **) realloc( result,
84                             sizeof(char *) * (num + 1) );
85
86                 result[num++] = (char *) ldap_strdup( buf );
87         }
88         if ( result == (char **) 0 )
89                 return( NULL );
90         result = (char **) realloc( result, sizeof(char *) * (num + 1) );
91         result[num] = NULL;
92
93         return( result );
94 }
95
96
97 static void
98 free_list( char **list )
99 {
100         int     i;
101
102         if ( list != NULL ) {
103                 for ( i = 0; list[ i ] != NULL; ++i ) {
104                         free( list[ i ] );
105                 }
106                 free( (char *)list );
107         }
108 }
109
110
111 static int
112 file_read( char *path, struct berval *bv )
113 {
114         FILE            *fp;
115         long            rlen;
116         int             eof;
117
118         if (( fp = fopen( path, "r" )) == NULL ) {
119                 perror( path );
120                 return( -1 );
121         }
122
123         if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
124                 perror( path );
125                 fclose( fp );
126                 return( -1 );
127         }
128
129         bv->bv_len = ftell( fp );
130
131         if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
132                 perror( "malloc" );
133                 fclose( fp );
134                 return( -1 );
135         }
136
137         if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
138                 perror( path );
139                 fclose( fp );
140                 return( -1 );
141         }
142
143         rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
144         eof = feof( fp );
145         fclose( fp );
146
147         if ( (unsigned long) rlen != bv->bv_len ) {
148                 perror( path );
149                 free( bv->bv_val );
150                 return( -1 );
151         }
152
153         return( bv->bv_len );
154 }
155
156
157 static LDAPMod **
158 get_modlist( char *prompt1, char *prompt2, char *prompt3 )
159 {
160         static char     buf[256];
161         int             num;
162         LDAPMod         tmp;
163         LDAPMod         **result;
164         struct berval   **bvals;
165
166         num = 0;
167         result = NULL;
168         while ( 1 ) {
169                 if ( prompt1 ) {
170                         getline( buf, sizeof(buf), stdin, prompt1 );
171                         tmp.mod_op = atoi( buf );
172
173                         if ( tmp.mod_op == -1 || buf[0] == '\0' )
174                                 break;
175                 }
176
177                 getline( buf, sizeof(buf), stdin, prompt2 );
178                 if ( buf[0] == '\0' )
179                         break;
180                 tmp.mod_type = ldap_strdup( buf );
181
182                 tmp.mod_values = get_list( prompt3 );
183
184                 if ( tmp.mod_values != NULL ) {
185                         int     i;
186
187                         for ( i = 0; tmp.mod_values[i] != NULL; ++i )
188                                 ;
189                         bvals = (struct berval **)calloc( i + 1,
190                             sizeof( struct berval *));
191                         for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
192                                 bvals[i] = (struct berval *)malloc(
193                                     sizeof( struct berval ));
194                                 if ( strncmp( tmp.mod_values[i], "{FILE}",
195                                     6 ) == 0 ) {
196                                         if ( file_read( tmp.mod_values[i] + 6,
197                                             bvals[i] ) < 0 ) {
198                                                 return( NULL );
199                                         }
200                                 } else {
201                                         bvals[i]->bv_val = tmp.mod_values[i];
202                                         bvals[i]->bv_len =
203                                             strlen( tmp.mod_values[i] );
204                                 }
205                         }
206                         tmp.mod_bvalues = bvals;
207                         tmp.mod_op |= LDAP_MOD_BVALUES;
208                 }
209
210                 if ( result == NULL )
211                         result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
212                 else
213                         result = (LDAPMod **) realloc( result,
214                             sizeof(LDAPMod *) * (num + 1) );
215
216                 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
217                 *(result[num]) = tmp;   /* struct copy */
218                 num++;
219         }
220         if ( result == NULL )
221                 return( NULL );
222         result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
223         result[num] = NULL;
224
225         return( result );
226 }
227
228
229 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
230 static int
231 bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
232         int freeit )
233 {
234         static char     dn[256], passwd[256];
235
236         if ( !freeit ) {
237 #ifdef HAVE_KERBEROS
238                 getline( dn, sizeof(dn), stdin,
239                     "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
240                 if (( *authmethodp = atoi( dn )) == 3 ) {
241                         *authmethodp = LDAP_AUTH_KRBV4;
242                 } else {
243                         *authmethodp |= 0x80;
244                 }
245 #else /* HAVE_KERBEROS */
246                 *authmethodp = LDAP_AUTH_SIMPLE;
247 #endif /* HAVE_KERBEROS */
248
249                 getline( dn, sizeof(dn), stdin, "re-bind dn? " );
250                 strcat( dn, dnsuffix );
251                 *dnp = dn;
252
253                 if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
254                         getline( passwd, sizeof(passwd), stdin,
255                             "re-bind password? " );
256                 } else {
257                         passwd[0] = '\0';
258                 }
259                 *passwdp = passwd;
260         }
261
262         return( LDAP_SUCCESS );
263 }
264 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
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                                 lber_debug = 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 = ldap_strdup( optarg );
327                         copyoptions = LBER_TO_FILE;
328                         break;
329
330                 case 'T':       /* only output ber's to given file */
331                         copyfname = ldap_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( 1 );
348         }
349         
350         printf( "%sldap_open( %s, %d )\n", cldapflg ? "c" : "",
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_open( host, port );
359         }
360
361         if ( ld == NULL ) {
362                 perror( "ldap_open" );
363                 exit(1);
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 ( 1 );
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 HAVE_KERBEROS
416                         getline( line, sizeof(line), stdin,
417                             "method (0->simple, 1->krbv41, 2->krbv42)? " );
418                         method = atoi( line ) | 0x80;
419 #else /* HAVE_KERBEROS */
420                         method = LDAP_AUTH_SIMPLE;
421 #endif /* HAVE_KERBEROS */
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 HAVE_KERBEROS
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 /* HAVE_KERBEROS */
450                         method = LDAP_AUTH_SIMPLE;
451 #endif /* HAVE_KERBEROS */
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                                 lber_debug = 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 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
549                         if ( !cldapflg )
550 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
551                         if ( !cldapflg && bound )
552 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
553                                 ldap_unbind( ld );
554                         exit( 0 );
555                         break;
556
557                 case 'r':       /* result or remove */
558                         switch ( command3 ) {
559                         case 's':       /* result */
560                                 getline( line, sizeof(line), stdin,
561                                     "msgid (-1=>any)? " );
562                                 if ( line[0] == '\0' )
563                                         id = -1;
564                                 else
565                                         id = atoi( line );
566                                 getline( line, sizeof(line), stdin,
567                                     "all (0=>any, 1=>all)? " );
568                                 if ( line[0] == '\0' )
569                                         all = 1;
570                                 else
571                                         all = atoi( line );
572                                 if (( msgtype = ldap_result( ld, id, all,
573                                     &timeout, &res )) < 1 ) {
574                                         ldap_perror( ld, "ldap_result" );
575                                         break;
576                                 }
577                                 printf( "\nresult: msgtype %d msgid %d\n",
578                                     msgtype, res->lm_msgid );
579                                 handle_result( ld, res );
580                                 res = NULLMSG;
581                                 break;
582
583                         case 'm':       /* remove */
584                                 getline( dn, sizeof(dn), stdin, "dn? " );
585                                 strcat( dn, dnsuffix );
586                                 if ( (id = ldap_delete( ld, dn )) == -1 )
587                                         ldap_perror( ld, "ldap_delete" );
588                                 else
589                                         printf( "Remove initiated with id %d\n",
590                                             id );
591                                 break;
592
593                         default:
594                                 printf( "Possibilities: [rem]ove, [res]ult\n" );
595                                 break;
596                         }
597                         break;
598
599                 case 's':       /* search */
600                         getline( dn, sizeof(dn), stdin, "searchbase? " );
601                         strcat( dn, dnsuffix );
602                         getline( line, sizeof(line), stdin,
603                             "scope (0=Base, 1=One Level, 2=Subtree)? " );
604                         scope = atoi( line );
605                         getline( filter, sizeof(filter), stdin,
606                             "search filter (e.g. sn=jones)? " );
607                         types = get_list( "attrs to return? " );
608                         getline( line, sizeof(line), stdin,
609                             "attrsonly (0=attrs&values, 1=attrs only)? " );
610                         attrsonly = atoi( line );
611
612                         if ( cldapflg ) {
613 #ifdef LDAP_CONNECTIONLESS
614                             getline( line, sizeof(line), stdin,
615                                 "Requestor DN (for logging)? " );
616                             if ( cldap_search_s( ld, dn, scope, filter, types,
617                                     attrsonly, &res, line ) != 0 ) {
618                                 ldap_perror( ld, "cldap_search_s" );
619                             } else {
620                                 printf( "\nresult: msgid %d\n",
621                                     res->lm_msgid );
622                                 handle_result( ld, res );
623                                 res = NULLMSG;
624                             }
625 #endif /* LDAP_CONNECTIONLESS */
626                         } else {
627                             if (( id = ldap_search( ld, dn, scope, filter,
628                                     types, attrsonly  )) == -1 ) {
629                                 ldap_perror( ld, "ldap_search" );
630                             } else {
631                                 printf( "Search initiated with id %d\n", id );
632                             }
633                         }
634                         free_list( types );
635                         break;
636
637                 case 't':       /* set timeout value */
638                         getline( line, sizeof(line), stdin, "timeout? " );
639                         timeout.tv_sec = atoi( line );
640                         break;
641
642                 case 'U':       /* set ufn search prefix */
643                         getline( line, sizeof(line), stdin, "ufn prefix? " );
644                         ldap_ufn_setprefix( ld, line );
645                         break;
646
647                 case 'u':       /* user friendly search w/optional timeout */
648                         getline( dn, sizeof(dn), stdin, "ufn? " );
649                         strcat( dn, dnsuffix );
650                         types = get_list( "attrs to return? " );
651                         getline( line, sizeof(line), stdin,
652                             "attrsonly (0=attrs&values, 1=attrs only)? " );
653                         attrsonly = atoi( line );
654
655                         if ( command2 == 't' ) {
656                                 id = ldap_ufn_search_c( ld, dn, types,
657                                     attrsonly, &res, ldap_ufn_timeout,
658                                     &timeout );
659                         } else {
660                                 id = ldap_ufn_search_s( ld, dn, types,
661                                     attrsonly, &res );
662                         }
663                         if ( res == NULL )
664                                 ldap_perror( ld, "ldap_ufn_search" );
665                         else {
666                                 printf( "\nresult: err %d\n", id );
667                                 handle_result( ld, res );
668                                 res = NULLMSG;
669                         }
670                         free_list( types );
671                         break;
672
673                 case 'l':       /* URL search */
674                         getline( line, sizeof(line), stdin,
675                             "attrsonly (0=attrs&values, 1=attrs only)? " );
676                         attrsonly = atoi( line );
677                         getline( line, sizeof(line), stdin, "LDAP URL? " );
678                         if (( id = ldap_url_search( ld, line, attrsonly  ))
679                                 == -1 ) {
680                             ldap_perror( ld, "ldap_url_search" );
681                         } else {
682                             printf( "URL search initiated with id %d\n", id );
683                         }
684                         break;
685
686                 case 'p':       /* parse LDAP URL */
687                         getline( line, sizeof(line), stdin, "LDAP URL? " );
688                         if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
689                             fprintf( stderr, "ldap_url_parse: error %d\n", i );
690                         } else {
691                             printf( "\t  host: " );
692                             if ( ludp->lud_host == NULL ) {
693                                 printf( "DEFAULT\n" );
694                             } else {
695                                 printf( "<%s>\n", ludp->lud_host );
696                             }
697                             printf( "\t  port: " );
698                             if ( ludp->lud_port == 0 ) {
699                                 printf( "DEFAULT\n" );
700                             } else {
701                                 printf( "%d\n", ludp->lud_port );
702                             }
703                             printf( "\t    dn: <%s>\n", ludp->lud_dn );
704                             printf( "\t attrs:" );
705                             if ( ludp->lud_attrs == NULL ) {
706                                 printf( " ALL" );
707                             } else {
708                                 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
709                                     printf( " <%s>", ludp->lud_attrs[ i ] );
710                                 }
711                             }
712                             printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
713                                 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
714                                 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
715                             printf( "\tfilter: <%s>\n", ludp->lud_filter );
716                             ldap_free_urldesc( ludp );
717                         }
718                             break;
719
720                 case 'n':       /* set dn suffix, for convenience */
721                         getline( line, sizeof(line), stdin, "DN suffix? " );
722                         strcpy( dnsuffix, line );
723                         break;
724
725                 case 'e':       /* enable cache */
726 #ifdef LDAP_NOCACHE
727                         printf( NOCACHEERRMSG );
728 #else /* LDAP_NOCACHE */
729                         getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
730                         i = atoi( line );
731                         getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
732                         if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
733                                 printf( "local cache is on\n" ); 
734                         } else {
735                                 printf( "ldap_enable_cache failed\n" ); 
736                         }
737 #endif /* LDAP_NOCACHE */
738                         break;
739
740                 case 'x':       /* uncache entry */
741 #ifdef LDAP_NOCACHE
742                         printf( NOCACHEERRMSG );
743 #else /* LDAP_NOCACHE */
744                         getline( line, sizeof(line), stdin, "DN? " );
745                         ldap_uncache_entry( ld, line );
746 #endif /* LDAP_NOCACHE */
747                         break;
748
749                 case 'X':       /* uncache request */
750 #ifdef LDAP_NOCACHE
751                         printf( NOCACHEERRMSG );
752 #else /* LDAP_NOCACHE */
753                         getline( line, sizeof(line), stdin, "request msgid? " );
754                         ldap_uncache_request( ld, atoi( line ));
755 #endif /* LDAP_NOCACHE */
756                         break;
757
758                 case 'o':       /* set ldap options */
759                         getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
760                         ld->ld_deref = atoi( line );
761                         getline( line, sizeof(line), stdin, "timelimit?" );
762                         ld->ld_timelimit = atoi( line );
763                         getline( line, sizeof(line), stdin, "sizelimit?" );
764                         ld->ld_sizelimit = atoi( line );
765
766                         LDAP_BOOL_ZERO(&ld->ld_options);
767
768 #ifdef STR_TRANSLATION
769                         getline( line, sizeof(line), stdin,
770                                 "Automatic translation of T.61 strings (0=no, 1=yes)?" );
771                         if ( atoi( line ) == 0 ) {
772                                 ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
773                         } else {
774                                 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
775 #ifdef LDAP_CHARSET_8859
776                                 getline( line, sizeof(line), stdin,
777                                         "Translate to/from ISO-8859 (0=no, 1=yes?" );
778                                 if ( atoi( line ) != 0 ) {
779                                         ldap_set_string_translators( ld,
780                                             ldap_8859_to_t61,
781                                             ldap_t61_to_8859 );
782                                 }
783 #endif /* LDAP_CHARSET_8859 */
784                         }
785 #endif /* STR_TRANSLATION */
786
787 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
788                         getline( line, sizeof(line), stdin,
789                                 "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
790                         if ( atoi( line ) != 0 ) {
791                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS);
792                         }
793 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
794
795 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
796                         getline( line, sizeof(line), stdin,
797                                 "Recognize and chase referrals (0=no, 1=yes)?" );
798                         if ( atoi( line ) != 0 ) {
799                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
800                                 getline( line, sizeof(line), stdin,
801                                         "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
802                                 if ( atoi( line ) != 0 ) {
803                                         ldap_set_rebind_proc( ld, bind_prompt );
804                                 }
805                         }
806 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
807                         break;
808
809                 case 'O':       /* set cache options */
810 #ifdef LDAP_NOCACHE
811                         printf( NOCACHEERRMSG );
812 #else /* LDAP_NOCACHE */
813                         getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
814                         switch( atoi( line )) {
815                         case 0:
816                                 ldap_set_cache_options( ld, 0 );
817                                 break;
818                         case 1:
819                                 ldap_set_cache_options( ld,
820                                         LDAP_CACHE_OPT_CACHENOERRS );
821                                 break;
822                         case 2:
823                                 ldap_set_cache_options( ld,
824                                         LDAP_CACHE_OPT_CACHEALLERRS );
825                                 break;
826                         default:
827                                 printf( "not a valid cache option\n" );
828                         }
829 #endif /* LDAP_NOCACHE */
830                         break;
831
832                 case '?':       /* help */
833     printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
834     printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
835     printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
836     printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
837     printf( "          [u]fn search  [ut]fn search with timeout\n" );
838     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
839     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
840     printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\n" );
841     printf( "          [?]help       [o]ptions         [O]cache options\n" );
842     printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
843                         break;
844
845                 default:
846                         printf( "Invalid command.  Type ? for help.\n" );
847                         break;
848                 }
849
850                 (void) memset( line, '\0', sizeof(line) );
851         }
852
853         return( 0 );
854 }
855
856 static void
857 handle_result( LDAP *ld, LDAPMessage *lm )
858 {
859         switch ( lm->lm_msgtype ) {
860         case LDAP_RES_COMPARE:
861                 printf( "Compare result\n" );
862                 print_ldap_result( ld, lm, "compare" );
863                 break;
864
865         case LDAP_RES_SEARCH_RESULT:
866                 printf( "Search result\n" );
867                 print_ldap_result( ld, lm, "search" );
868                 break;
869
870         case LDAP_RES_SEARCH_ENTRY:
871                 printf( "Search entry\n" );
872                 print_search_entry( ld, lm );
873                 break;
874
875         case LDAP_RES_ADD:
876                 printf( "Add result\n" );
877                 print_ldap_result( ld, lm, "add" );
878                 break;
879
880         case LDAP_RES_DELETE:
881                 printf( "Delete result\n" );
882                 print_ldap_result( ld, lm, "delete" );
883                 break;
884
885         case LDAP_RES_MODRDN:
886                 printf( "ModRDN result\n" );
887                 print_ldap_result( ld, lm, "modrdn" );
888                 break;
889
890         case LDAP_RES_BIND:
891                 printf( "Bind result\n" );
892                 print_ldap_result( ld, lm, "bind" );
893                 break;
894
895         default:
896                 printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
897                 print_ldap_result( ld, lm, "unknown" );
898         }
899 }
900
901 static void
902 print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
903 {
904         ldap_result2error( ld, lm, 1 );
905         ldap_perror( ld, s );
906 /*
907         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
908                 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
909         if ( NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
910                 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
911 */
912 }
913
914 static void
915 print_search_entry( LDAP *ld, LDAPMessage *res )
916 {
917         BerElement      *ber;
918         char            *a, *dn, *ufn;
919         struct berval   **vals;
920         int             i;
921         LDAPMessage     *e;
922
923         for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
924             e = ldap_next_entry( ld, e ) ) {
925                 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
926                         break;
927
928                 dn = ldap_get_dn( ld, e );
929                 printf( "\tDN: %s\n", dn );
930
931                 ufn = ldap_dn2ufn( dn );
932                 printf( "\tUFN: %s\n", ufn );
933
934                 free( dn );
935                 free( ufn );
936
937                 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
938                     a = ldap_next_attribute( ld, e, ber ) ) {
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                                 for ( i = 0; vals[i] != NULL; i++ ) {
945                                         int     j, nonascii;
946
947                                         nonascii = 0;
948                                         for ( j = 0; (unsigned long) j < vals[i]->bv_len; j++ )
949                                                 if ( !isascii( vals[i]->bv_val[j] ) ) {
950                                                         nonascii = 1;
951                                                         break;
952                                                 }
953
954                                         if ( nonascii ) {
955                                                 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
956 #ifdef BPRINT_NONASCII
957                                                 lber_bprint( vals[i]->bv_val,
958                                                     vals[i]->bv_len );
959 #endif /* BPRINT_NONASCII */
960                                                 continue;
961                                         }
962                                         printf( "\t\t\tlength (%ld) %s\n",
963                                             vals[i]->bv_len, vals[i]->bv_val );
964                                 }
965                                 ber_bvecfree( vals );
966                         }
967                 }
968         }
969
970         if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
971             || res->lm_chain != NULLMSG )
972                 print_ldap_result( ld, res, "search" );
973 }