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