]> git.sur5r.net Git - openldap/blob - libraries/libldap/test.c
rename ldap_pvt_init_utils() to ldap_int_utils_init() and provide
[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 *) 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 = 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 static int
230 bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
231         int freeit )
232 {
233         static char     dn[256], passwd[256];
234
235         if ( !freeit ) {
236 #ifdef HAVE_KERBEROS
237                 getline( dn, sizeof(dn), stdin,
238                     "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
239                 if (( *authmethodp = atoi( dn )) == 3 ) {
240                         *authmethodp = LDAP_AUTH_KRBV4;
241                 } else {
242                         *authmethodp |= 0x80;
243                 }
244 #else /* HAVE_KERBEROS */
245                 *authmethodp = LDAP_AUTH_SIMPLE;
246 #endif /* HAVE_KERBEROS */
247
248                 getline( dn, sizeof(dn), stdin, "re-bind dn? " );
249                 strcat( dn, dnsuffix );
250                 *dnp = dn;
251
252                 if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
253                         getline( passwd, sizeof(passwd), stdin,
254                             "re-bind password? " );
255                 } else {
256                         passwd[0] = '\0';
257                 }
258                 *passwdp = passwd;
259         }
260
261         return( LDAP_SUCCESS );
262 }
263
264
265 int
266 main( int argc, char **argv )
267 {
268         LDAP            *ld = NULL;
269         int             i, c, port, cldapflg, errflg, method, id, msgtype;
270         char            line[256], command1, command2, command3;
271         char            passwd[64], dn[256], rdn[64], attr[64], value[256];
272         char            filter[256], *host, **types;
273         char            **exdn;
274         char            *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
275         int             bound, all, scope, attrsonly;
276         LDAPMessage     *res;
277         LDAPMod         **mods, **attrs;
278         struct timeval  timeout;
279         char            *copyfname = NULL;
280         int             copyoptions = 0;
281         LDAPURLDesc     *ludp;
282
283         host = NULL;
284         port = LDAP_PORT;
285         dnsuffix = "";
286         cldapflg = errflg = 0;
287
288         while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
289                 switch( c ) {
290                 case 'u':
291 #ifdef LDAP_CONNECTIONLESS
292                         cldapflg++;
293 #else /* LDAP_CONNECTIONLESS */
294                         printf( "Compile with -DLDAP_CONNECTIONLESS for UDP support\n" );
295 #endif /* LDAP_CONNECTIONLESS */
296                         break;
297
298                 case 'd':
299 #ifdef LDAP_DEBUG
300                         ldap_debug = atoi( optarg );
301 #ifdef LBER_DEBUG
302                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
303                                 lber_debug = ldap_debug;
304                         }
305 #endif
306 #else
307                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
308 #endif
309                         break;
310
311                 case 'h':
312                         host = optarg;
313                         break;
314
315                 case 's':
316                         dnsuffix = optarg;
317                         break;
318
319                 case 'p':
320                         port = atoi( optarg );
321                         break;
322
323                 case 't':       /* copy ber's to given file */
324                         copyfname = strdup( optarg );
325                         copyoptions = LBER_TO_FILE;
326                         break;
327
328                 case 'T':       /* only output ber's to given file */
329                         copyfname = strdup( optarg );
330                         copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
331                         break;
332
333                 default:
334                     ++errflg;
335                 }
336         }
337
338         if ( host == NULL && optind == argc - 1 ) {
339                 host = argv[ optind ];
340                 ++optind;
341         }
342
343         if ( errflg || optind < argc - 1 ) {
344                 fprintf( stderr, usage, argv[ 0 ] );
345                 exit( 1 );
346         }
347         
348         printf( "%sldap_open( %s, %d )\n", cldapflg ? "c" : "",
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_open( host, port );
357         }
358
359         if ( ld == NULL ) {
360                 perror( "ldap_open" );
361                 exit(1);
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 ( 1 );
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 HAVE_KERBEROS
414                         getline( line, sizeof(line), stdin,
415                             "method (0->simple, 1->krbv41, 2->krbv42)? " );
416                         method = atoi( line ) | 0x80;
417 #else /* HAVE_KERBEROS */
418                         method = LDAP_AUTH_SIMPLE;
419 #endif /* HAVE_KERBEROS */
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 HAVE_KERBEROS
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 /* HAVE_KERBEROS */
448                         method = LDAP_AUTH_SIMPLE;
449 #endif /* HAVE_KERBEROS */
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                                 lber_debug = 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( 0 );
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 = NULLMSG;
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 = NULLMSG;
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 = NULLMSG;
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%x\n", lm->lm_msgtype );
891                 print_ldap_result( ld, lm, "unknown" );
892         }
893 }
894
895 static void
896 print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
897 {
898         ldap_result2error( ld, lm, 1 );
899         ldap_perror( ld, s );
900 /*
901         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
902                 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
903         if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
904                 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
905 */
906 }
907
908 static void
909 print_search_entry( LDAP *ld, LDAPMessage *res )
910 {
911         LDAPMessage     *e;
912
913         for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
914             e = ldap_next_entry( ld, e ) )
915         {
916                 BerElement      *ber = NULL;
917                 char *a, *dn, *ufn;
918
919                 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
920                         break;
921
922                 dn = ldap_get_dn( ld, e );
923                 printf( "\tDN: %s\n", dn );
924
925                 ufn = ldap_dn2ufn( dn );
926                 printf( "\tUFN: %s\n", ufn );
927
928                 free( dn );
929                 free( ufn );
930
931                 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
932                     a = ldap_next_attribute( ld, e, ber ) )
933                 {
934                         struct berval   **vals;
935
936                         printf( "\t\tATTR: %s\n", a );
937                         if ( (vals = ldap_get_values_len( ld, e, a ))
938                             == NULL ) {
939                                 printf( "\t\t\t(no values)\n" );
940                         } else {
941                                 int i;
942                                 for ( i = 0; vals[i] != NULL; i++ ) {
943                                         int     j, nonascii;
944
945                                         nonascii = 0;
946                                         for ( j = 0; (unsigned long) j < vals[i]->bv_len; j++ )
947                                                 if ( !isascii( vals[i]->bv_val[j] ) ) {
948                                                         nonascii = 1;
949                                                         break;
950                                                 }
951
952                                         if ( nonascii ) {
953                                                 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
954 #ifdef BPRINT_NONASCII
955                                                 lber_bprint( vals[i]->bv_val,
956                                                     vals[i]->bv_len );
957 #endif /* BPRINT_NONASCII */
958                                                 continue;
959                                         }
960                                         printf( "\t\t\tlength (%ld) %s\n",
961                                             vals[i]->bv_len, vals[i]->bv_val );
962                                 }
963                                 ber_bvecfree( vals );
964                         }
965                 }
966
967                 if(ber != NULL) {
968                         ber_free( ber, 0 );
969                 }
970         }
971
972         if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
973             || res->lm_chain != NULLMSG )
974                 print_ldap_result( ld, res, "search" );
975 }