]> git.sur5r.net Git - openldap/blob - libraries/libldap/test.c
happy new year
[openldap] / libraries / libldap / test.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdlib.h>
21
22 #include <ac/ctype.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26 #include <ac/unistd.h>
27
28 #include <sys/stat.h>
29
30 #ifdef HAVE_SYS_FILE_H
31 #include <sys/file.h>
32 #endif
33 #ifdef HAVE_IO_H
34 #include <io.h>
35 #endif
36
37 #include <fcntl.h>
38
39 /* including the "internal" defs is legit and nec. since this test routine has 
40  * a-priori knowledge of libldap internal workings.
41  * hodges@stanford.edu 5-Feb-96
42  */
43 #include "ldap-int.h"
44
45 /* local functions */
46 static char *get_line LDAP_P(( char *line, int len, FILE *fp, const char *prompt ));
47 static char **get_list LDAP_P(( const char *prompt ));
48 static int file_read LDAP_P(( const char *path, struct berval *bv ));
49 static LDAPMod **get_modlist LDAP_P(( const char *prompt1,
50         const char *prompt2, const char *prompt3 ));
51 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
52 static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm,
53         const char *s ));
54 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
55 static void free_list LDAP_P(( char **list ));
56
57 static char *dnsuffix;
58
59 static char *
60 get_line( char *line, int len, FILE *fp, const char *prompt )
61 {
62         fputs(prompt, stdout);
63
64         if ( fgets( line, len, fp ) == NULL )
65                 return( NULL );
66
67         line[ strlen( line ) - 1 ] = '\0';
68
69         return( line );
70 }
71
72 static char **
73 get_list( const char *prompt )
74 {
75         static char     buf[256];
76         int             num;
77         char            **result;
78
79         num = 0;
80         result = (char **) 0;
81         while ( 1 ) {
82                 get_line( buf, sizeof(buf), stdin, prompt );
83
84                 if ( *buf == '\0' )
85                         break;
86
87                 if ( result == (char **) 0 )
88                         result = (char **) malloc( sizeof(char *) );
89                 else
90                         result = (char **) realloc( result,
91                             sizeof(char *) * (num + 1) );
92
93                 result[num++] = (char *) strdup( buf );
94         }
95         if ( result == (char **) 0 )
96                 return( NULL );
97         result = (char **) realloc( result, sizeof(char *) * (num + 1) );
98         result[num] = NULL;
99
100         return( result );
101 }
102
103
104 static void
105 free_list( char **list )
106 {
107         int     i;
108
109         if ( list != NULL ) {
110                 for ( i = 0; list[ i ] != NULL; ++i ) {
111                         free( list[ i ] );
112                 }
113                 free( (char *)list );
114         }
115 }
116
117
118 static int
119 file_read( const char *path, struct berval *bv )
120 {
121         FILE            *fp;
122         ber_slen_t      rlen;
123         int             eof;
124
125         if (( fp = fopen( path, "r" )) == NULL ) {
126                 perror( path );
127                 return( -1 );
128         }
129
130         if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
131                 perror( path );
132                 fclose( fp );
133                 return( -1 );
134         }
135
136         bv->bv_len = ftell( fp );
137
138         if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
139                 perror( "malloc" );
140                 fclose( fp );
141                 return( -1 );
142         }
143
144         if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
145                 perror( path );
146                 fclose( fp );
147                 return( -1 );
148         }
149
150         rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
151         eof = feof( fp );
152         fclose( fp );
153
154         if ( (ber_len_t) rlen != bv->bv_len ) {
155                 perror( path );
156                 free( bv->bv_val );
157                 return( -1 );
158         }
159
160         return( bv->bv_len );
161 }
162
163
164 static LDAPMod **
165 get_modlist(
166         const char *prompt1,
167         const char *prompt2,
168         const char *prompt3 )
169 {
170         static char     buf[256];
171         int             num;
172         LDAPMod         tmp = { 0 };
173         LDAPMod         **result;
174         struct berval   **bvals;
175
176         num = 0;
177         result = NULL;
178         while ( 1 ) {
179                 if ( prompt1 ) {
180                         get_line( buf, sizeof(buf), stdin, prompt1 );
181                         tmp.mod_op = atoi( buf );
182
183                         if ( tmp.mod_op == -1 || buf[0] == '\0' )
184                                 break;
185                 }
186
187                 get_line( buf, sizeof(buf), stdin, prompt2 );
188                 if ( buf[0] == '\0' )
189                         break;
190                 tmp.mod_type = strdup( buf );
191
192                 tmp.mod_values = get_list( prompt3 );
193
194                 if ( tmp.mod_values != NULL ) {
195                         int     i;
196
197                         for ( i = 0; tmp.mod_values[i] != NULL; ++i )
198                                 ;
199                         bvals = (struct berval **)calloc( i + 1,
200                             sizeof( struct berval *));
201                         for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
202                                 bvals[i] = (struct berval *)malloc(
203                                     sizeof( struct berval ));
204                                 if ( strncmp( tmp.mod_values[i], "{FILE}",
205                                     6 ) == 0 ) {
206                                         if ( file_read( tmp.mod_values[i] + 6,
207                                             bvals[i] ) < 0 ) {
208                                                 free( bvals );
209                                                 for ( i = 0; i<num; i++ )
210                                                         free( result[ i ] );
211                                                 free( result );
212                                                 return( NULL );
213                                         }
214                                 } else {
215                                         bvals[i]->bv_val = tmp.mod_values[i];
216                                         bvals[i]->bv_len =
217                                             strlen( tmp.mod_values[i] );
218                                 }
219                         }
220                         tmp.mod_bvalues = bvals;
221                         tmp.mod_op |= LDAP_MOD_BVALUES;
222                 }
223
224                 if ( result == NULL )
225                         result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
226                 else
227                         result = (LDAPMod **) realloc( result,
228                             sizeof(LDAPMod *) * (num + 1) );
229
230                 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
231                 *(result[num]) = tmp;   /* struct copy */
232                 num++;
233         }
234         if ( result == NULL )
235                 return( NULL );
236         result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
237         result[num] = NULL;
238
239         return( result );
240 }
241
242
243 static int
244 bind_prompt( LDAP *ld,
245         LDAP_CONST char *url,
246         ber_tag_t request, ber_int_t msgid,
247         void *params )
248 {
249         static char     dn[256], passwd[256];
250         int     authmethod;
251
252         printf("rebind for request=%ld msgid=%ld url=%s\n",
253                 request, (long) msgid, url );
254
255 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
256                 get_line( dn, sizeof(dn), stdin,
257                     "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
258         if (( authmethod = atoi( dn )) == 3 ) {
259                 authmethod = LDAP_AUTH_KRBV4;
260                 } else {
261                 authmethod |= 0x80;
262                 }
263 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
264         authmethod = LDAP_AUTH_SIMPLE;
265 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
266
267                 get_line( dn, sizeof(dn), stdin, "re-bind dn? " );
268                 strcat( dn, dnsuffix );
269
270         if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
271                         get_line( passwd, sizeof(passwd), stdin,
272                             "re-bind password? " );
273                 } else {
274                         passwd[0] = '\0';
275                 }
276
277         return ldap_bind_s( ld, dn, passwd, authmethod);
278 }
279
280
281 int
282 main( int argc, char **argv )
283 {
284         LDAP            *ld = NULL;
285         int             i, c, port, errflg, method, id, msgtype;
286         char            line[256], command1, command2, command3;
287         char            passwd[64], dn[256], rdn[64], attr[64], value[256];
288         char            filter[256], *host, **types;
289         char            **exdn;
290         char            *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
291         int             bound, all, scope, attrsonly;
292         LDAPMessage     *res;
293         LDAPMod         **mods, **attrs;
294         struct timeval  timeout;
295         char            *copyfname = NULL;
296         int             copyoptions = 0;
297         LDAPURLDesc     *ludp;
298
299         host = NULL;
300         port = LDAP_PORT;
301         dnsuffix = "";
302         errflg = 0;
303
304         while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
305                 switch( c ) {
306                 case 'd':
307 #ifdef LDAP_DEBUG
308                         ldap_debug = atoi( optarg );
309 #ifdef LBER_DEBUG
310                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
311                                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
312                         }
313 #endif
314 #else
315                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
316 #endif
317                         break;
318
319                 case 'h':
320                         host = optarg;
321                         break;
322
323                 case 's':
324                         dnsuffix = optarg;
325                         break;
326
327                 case 'p':
328                         port = atoi( optarg );
329                         break;
330
331                 case 't':       /* copy ber's to given file */
332                         copyfname = strdup( optarg );
333 /*                      copyoptions = LBER_TO_FILE; */
334                         break;
335
336                 case 'T':       /* only output ber's to given file */
337                         copyfname = strdup( optarg );
338 /*                      copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
339                         break;
340
341                 default:
342                     ++errflg;
343                 }
344         }
345
346         if ( host == NULL && optind == argc - 1 ) {
347                 host = argv[ optind ];
348                 ++optind;
349         }
350
351         if ( errflg || optind < argc - 1 ) {
352                 fprintf( stderr, usage, argv[ 0 ] );
353                 exit( EXIT_FAILURE );
354         }
355         
356         printf( "ldap_init( %s, %d )\n",
357                 host == NULL ? "(null)" : host, port );
358
359         ld = ldap_init( host, port );
360
361         if ( ld == NULL ) {
362                 perror( "ldap_init" );
363                 exit( EXIT_FAILURE );
364         }
365
366         if ( copyfname != NULL ) {
367                 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
368                     0600 ))  == -1 ) {
369                         perror( copyfname );
370                         exit ( EXIT_FAILURE );
371                 }
372                 ld->ld_sb->sb_options = copyoptions;
373         }
374
375         bound = 0;
376         timeout.tv_sec = 0;
377         timeout.tv_usec = 0;
378
379         (void) memset( line, '\0', sizeof(line) );
380         while ( get_line( 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                                 get_line( 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                                 get_line( line, sizeof(line), stdin, "msgid? " );
403                                 id = atoi( line );
404                                 if ( ldap_abandon( ld, id ) != 0 )
405                                         ldap_perror( ld, "ldap_abandon" );
406                                 else
407                                         printf( "Abandon successful\n" );
408                                 break;
409                         default:
410                                 printf( "Possibilities: [ad]d, [ab]ort\n" );
411                         }
412                         break;
413
414                 case 'b':       /* asynch bind */
415 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
416                         get_line( line, sizeof(line), stdin,
417                             "method (0->simple, 1->krbv41, 2->krbv42)? " );
418                         method = atoi( line ) | 0x80;
419 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
420                         method = LDAP_AUTH_SIMPLE;
421 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
422                         get_line( dn, sizeof(dn), stdin, "dn? " );
423                         strcat( dn, dnsuffix );
424
425                         if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
426                                 get_line( passwd, sizeof(passwd), stdin,
427                                     "password? " );
428                         else
429                                 passwd[0] = '\0';
430
431                         if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
432                                 fprintf( stderr, "ldap_bind failed\n" );
433                                 ldap_perror( ld, "ldap_bind" );
434                         } else {
435                                 printf( "Bind initiated\n" );
436                                 bound = 1;
437                         }
438                         break;
439
440                 case 'B':       /* synch bind */
441 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
442                         get_line( line, sizeof(line), stdin,
443                             "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
444                         method = atoi( line );
445                         if ( method == 3 )
446                                 method = LDAP_AUTH_KRBV4;
447                         else
448                                 method = method | 0x80;
449 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
450                         method = LDAP_AUTH_SIMPLE;
451 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
452                         get_line( dn, sizeof(dn), stdin, "dn? " );
453                         strcat( dn, dnsuffix );
454
455                         if ( dn[0] != '\0' )
456                                 get_line( 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                         get_line( dn, sizeof(dn), stdin, "dn? " );
473                         strcat( dn, dnsuffix );
474                         get_line( attr, sizeof(attr), stdin, "attr? " );
475                         get_line( 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                         get_line( line, sizeof(line), stdin, "debug level? " );
486                         ldap_debug = atoi( line );
487 #ifdef LBER_DEBUG
488                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
489                                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
490                         }
491 #endif
492 #else
493                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
494 #endif
495                         break;
496
497                 case 'E':       /* explode a dn */
498                         get_line( 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                         get_line( line, sizeof(line), stdin, "msgid? " );
507                         ld->ld_msgid = atoi( line );
508                         break;
509
510                 case 'v':       /* set version number */
511                         get_line( 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                                 get_line( 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                                 get_line( dn, sizeof(dn), stdin, "dn? " );
531                                 strcat( dn, dnsuffix );
532                                 get_line( 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                         ldap_unbind( ld );
545                         exit( EXIT_SUCCESS );
546                         break;
547
548                 case 'r':       /* result or remove */
549                         switch ( command3 ) {
550                         case 's':       /* result */
551                                 get_line( line, sizeof(line), stdin,
552                                     "msgid (-1=>any)? " );
553                                 if ( line[0] == '\0' )
554                                         id = -1;
555                                 else
556                                         id = atoi( line );
557                                 get_line( line, sizeof(line), stdin,
558                                     "all (0=>any, 1=>all)? " );
559                                 if ( line[0] == '\0' )
560                                         all = 1;
561                                 else
562                                         all = atoi( line );
563                                 if (( msgtype = ldap_result( ld, id, all,
564                                     &timeout, &res )) < 1 ) {
565                                         ldap_perror( ld, "ldap_result" );
566                                         break;
567                                 }
568                                 printf( "\nresult: msgtype %d msgid %d\n",
569                                     msgtype, res->lm_msgid );
570                                 handle_result( ld, res );
571                                 res = NULL;
572                                 break;
573
574                         case 'm':       /* remove */
575                                 get_line( dn, sizeof(dn), stdin, "dn? " );
576                                 strcat( dn, dnsuffix );
577                                 if ( (id = ldap_delete( ld, dn )) == -1 )
578                                         ldap_perror( ld, "ldap_delete" );
579                                 else
580                                         printf( "Remove initiated with id %d\n",
581                                             id );
582                                 break;
583
584                         default:
585                                 printf( "Possibilities: [rem]ove, [res]ult\n" );
586                                 break;
587                         }
588                         break;
589
590                 case 's':       /* search */
591                         get_line( dn, sizeof(dn), stdin, "searchbase? " );
592                         strcat( dn, dnsuffix );
593                         get_line( line, sizeof(line), stdin,
594                             "scope (0=baseObject, 1=oneLevel, 2=subtree, 3=children)? " );
595                         scope = atoi( line );
596                         get_line( filter, sizeof(filter), stdin,
597                             "search filter (e.g. sn=jones)? " );
598                         types = get_list( "attrs to return? " );
599                         get_line( line, sizeof(line), stdin,
600                             "attrsonly (0=attrs&values, 1=attrs only)? " );
601                         attrsonly = atoi( line );
602
603                             if (( id = ldap_search( ld, dn, scope, filter,
604                                     types, attrsonly  )) == -1 ) {
605                                 ldap_perror( ld, "ldap_search" );
606                             } else {
607                                 printf( "Search initiated with id %d\n", id );
608                             }
609                         free_list( types );
610                         break;
611
612                 case 't':       /* set timeout value */
613                         get_line( line, sizeof(line), stdin, "timeout? " );
614                         timeout.tv_sec = atoi( line );
615                         break;
616
617                 case 'p':       /* parse LDAP URL */
618                         get_line( line, sizeof(line), stdin, "LDAP URL? " );
619                         if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
620                             fprintf( stderr, "ldap_url_parse: error %d\n", i );
621                         } else {
622                             printf( "\t  host: " );
623                             if ( ludp->lud_host == NULL ) {
624                                 printf( "DEFAULT\n" );
625                             } else {
626                                 printf( "<%s>\n", ludp->lud_host );
627                             }
628                             printf( "\t  port: " );
629                             if ( ludp->lud_port == 0 ) {
630                                 printf( "DEFAULT\n" );
631                             } else {
632                                 printf( "%d\n", ludp->lud_port );
633                             }
634                             printf( "\t    dn: <%s>\n", ludp->lud_dn );
635                             printf( "\t attrs:" );
636                             if ( ludp->lud_attrs == NULL ) {
637                                 printf( " ALL" );
638                             } else {
639                                 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
640                                     printf( " <%s>", ludp->lud_attrs[ i ] );
641                                 }
642                             }
643                             printf( "\n\t scope: %s\n",
644                                         ludp->lud_scope == LDAP_SCOPE_BASE ? "baseObject"
645                                         : ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "oneLevel"
646                                         : ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "subtree"
647 #ifdef LDAP_SCOPE_SUBORDINATE
648                                         : ludp->lud_scope == LDAP_SCOPE_SUBORDINATE ? "children"
649 #endif
650                                         : "**invalid**" );
651                             printf( "\tfilter: <%s>\n", ludp->lud_filter );
652                             ldap_free_urldesc( ludp );
653                         }
654                             break;
655
656                 case 'n':       /* set dn suffix, for convenience */
657                         get_line( line, sizeof(line), stdin, "DN suffix? " );
658                         strcpy( dnsuffix, line );
659                         break;
660
661                 case 'o':       /* set ldap options */
662                         get_line( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
663                         ld->ld_deref = atoi( line );
664                         get_line( line, sizeof(line), stdin, "timelimit?" );
665                         ld->ld_timelimit = atoi( line );
666                         get_line( line, sizeof(line), stdin, "sizelimit?" );
667                         ld->ld_sizelimit = atoi( line );
668
669                         LDAP_BOOL_ZERO(&ld->ld_options);
670
671                         get_line( line, sizeof(line), stdin,
672                                 "Recognize and chase referrals (0=no, 1=yes)?" );
673                         if ( atoi( line ) != 0 ) {
674                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
675                                 get_line( line, sizeof(line), stdin,
676                                         "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
677                                 if ( atoi( line ) != 0 ) {
678                                         ldap_set_rebind_proc( ld, bind_prompt, NULL );
679                                 }
680                         }
681                         break;
682
683                 case '?':       /* help */
684                         printf(
685 "Commands: [ad]d         [ab]andon         [b]ind\n"
686 "          [B]ind async  [c]ompare\n"
687 "          [modi]fy      [modr]dn          [rem]ove\n"
688 "          [res]ult      [s]earch          [q]uit/unbind\n\n"
689 "          [d]ebug       set ms[g]id\n"
690 "          d[n]suffix    [t]imeout         [v]ersion\n"
691 "          [?]help       [o]ptions"
692 "          [E]xplode dn  [p]arse LDAP URL\n" );
693                         break;
694
695                 default:
696                         printf( "Invalid command.  Type ? for help.\n" );
697                         break;
698                 }
699
700                 (void) memset( line, '\0', sizeof(line) );
701         }
702
703         return( 0 );
704 }
705
706 static void
707 handle_result( LDAP *ld, LDAPMessage *lm )
708 {
709         switch ( lm->lm_msgtype ) {
710         case LDAP_RES_COMPARE:
711                 printf( "Compare result\n" );
712                 print_ldap_result( ld, lm, "compare" );
713                 break;
714
715         case LDAP_RES_SEARCH_RESULT:
716                 printf( "Search result\n" );
717                 print_ldap_result( ld, lm, "search" );
718                 break;
719
720         case LDAP_RES_SEARCH_ENTRY:
721                 printf( "Search entry\n" );
722                 print_search_entry( ld, lm );
723                 break;
724
725         case LDAP_RES_ADD:
726                 printf( "Add result\n" );
727                 print_ldap_result( ld, lm, "add" );
728                 break;
729
730         case LDAP_RES_DELETE:
731                 printf( "Delete result\n" );
732                 print_ldap_result( ld, lm, "delete" );
733                 break;
734
735         case LDAP_RES_MODRDN:
736                 printf( "ModRDN result\n" );
737                 print_ldap_result( ld, lm, "modrdn" );
738                 break;
739
740         case LDAP_RES_BIND:
741                 printf( "Bind result\n" );
742                 print_ldap_result( ld, lm, "bind" );
743                 break;
744
745         default:
746                 printf( "Unknown result type 0x%lx\n",
747                         (unsigned long) lm->lm_msgtype );
748                 print_ldap_result( ld, lm, "unknown" );
749         }
750 }
751
752 static void
753 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
754 {
755         ldap_result2error( ld, lm, 1 );
756         ldap_perror( ld, s );
757 /*
758         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
759                 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
760         if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
761                 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
762 */
763 }
764
765 static void
766 print_search_entry( LDAP *ld, LDAPMessage *res )
767 {
768         LDAPMessage     *e;
769
770         for ( e = ldap_first_entry( ld, res ); e != NULL;
771             e = ldap_next_entry( ld, e ) )
772         {
773                 BerElement      *ber = NULL;
774                 char *a, *dn, *ufn;
775
776                 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
777                         break;
778
779                 dn = ldap_get_dn( ld, e );
780                 printf( "\tDN: %s\n", dn );
781
782                 ufn = ldap_dn2ufn( dn );
783                 printf( "\tUFN: %s\n", ufn );
784
785                 free( dn );
786                 free( ufn );
787
788                 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
789                     a = ldap_next_attribute( ld, e, ber ) )
790                 {
791                         struct berval   **vals;
792
793                         printf( "\t\tATTR: %s\n", a );
794                         if ( (vals = ldap_get_values_len( ld, e, a ))
795                             == NULL ) {
796                                 printf( "\t\t\t(no values)\n" );
797                         } else {
798                                 int i;
799                                 for ( i = 0; vals[i] != NULL; i++ ) {
800                                         int     j, nonascii;
801
802                                         nonascii = 0;
803                                         for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
804                                                 if ( !isascii( vals[i]->bv_val[j] ) ) {
805                                                         nonascii = 1;
806                                                         break;
807                                                 }
808
809                                         if ( nonascii ) {
810                                                 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
811 #ifdef BPRINT_NONASCII
812                                                 ber_bprint( vals[i]->bv_val,
813                                                     vals[i]->bv_len );
814 #endif /* BPRINT_NONASCII */
815                                                 continue;
816                                         }
817                                         printf( "\t\t\tlength (%ld) %s\n",
818                                             vals[i]->bv_len, vals[i]->bv_val );
819                                 }
820                                 ber_bvecfree( vals );
821                         }
822                 }
823
824                 if(ber != NULL) {
825                         ber_free( ber, 0 );
826                 }
827         }
828
829         if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
830             || res->lm_chain != NULL )
831                 print_ldap_result( ld, res, "search" );
832 }