]> git.sur5r.net Git - openldap/blob - libraries/libldap/test.c
Sync with HEAD
[openldap] / libraries / libldap / test.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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         printf(prompt);
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;
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                                                 return( NULL );
209                                         }
210                                 } else {
211                                         bvals[i]->bv_val = tmp.mod_values[i];
212                                         bvals[i]->bv_len =
213                                             strlen( tmp.mod_values[i] );
214                                 }
215                         }
216                         tmp.mod_bvalues = bvals;
217                         tmp.mod_op |= LDAP_MOD_BVALUES;
218                 }
219
220                 if ( result == NULL )
221                         result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
222                 else
223                         result = (LDAPMod **) realloc( result,
224                             sizeof(LDAPMod *) * (num + 1) );
225
226                 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
227                 *(result[num]) = tmp;   /* struct copy */
228                 num++;
229         }
230         if ( result == NULL )
231                 return( NULL );
232         result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
233         result[num] = NULL;
234
235         return( result );
236 }
237
238
239 static int
240 bind_prompt( LDAP *ld,
241         LDAP_CONST char *url,
242         ber_tag_t request, ber_int_t msgid,
243         void *params )
244 {
245         static char     dn[256], passwd[256];
246         int     authmethod;
247
248         printf("rebind for request=%ld msgid=%ld url=%s\n",
249                 request, (long) msgid, url );
250
251 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
252                 get_line( dn, sizeof(dn), stdin,
253                     "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
254         if (( authmethod = atoi( dn )) == 3 ) {
255                 authmethod = LDAP_AUTH_KRBV4;
256                 } else {
257                 authmethod |= 0x80;
258                 }
259 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
260         authmethod = LDAP_AUTH_SIMPLE;
261 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
262
263                 get_line( dn, sizeof(dn), stdin, "re-bind dn? " );
264                 strcat( dn, dnsuffix );
265
266         if ( authmethod == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
267                         get_line( passwd, sizeof(passwd), stdin,
268                             "re-bind password? " );
269                 } else {
270                         passwd[0] = '\0';
271                 }
272
273         return ldap_bind_s( ld, dn, passwd, authmethod);
274 }
275
276
277 int
278 main( int argc, char **argv )
279 {
280         LDAP            *ld = NULL;
281         int             i, c, port, errflg, method, id, msgtype;
282         char            line[256], command1, command2, command3;
283         char            passwd[64], dn[256], rdn[64], attr[64], value[256];
284         char            filter[256], *host, **types;
285         char            **exdn;
286         char            *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
287         int             bound, all, scope, attrsonly;
288         LDAPMessage     *res;
289         LDAPMod         **mods, **attrs;
290         struct timeval  timeout;
291         char            *copyfname = NULL;
292         int             copyoptions = 0;
293         LDAPURLDesc     *ludp;
294
295         host = NULL;
296         port = LDAP_PORT;
297         dnsuffix = "";
298         errflg = 0;
299
300         while (( c = getopt( argc, argv, "h:d:s:p:t:T:" )) != -1 ) {
301                 switch( c ) {
302                 case 'd':
303 #ifdef LDAP_DEBUG
304                         ldap_debug = atoi( optarg );
305 #ifdef LBER_DEBUG
306                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
307                                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
308                         }
309 #endif
310 #else
311                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
312 #endif
313                         break;
314
315                 case 'h':
316                         host = optarg;
317                         break;
318
319                 case 's':
320                         dnsuffix = optarg;
321                         break;
322
323                 case 'p':
324                         port = atoi( optarg );
325                         break;
326
327                 case 't':       /* copy ber's to given file */
328                         copyfname = strdup( optarg );
329 /*                      copyoptions = LBER_TO_FILE; */
330                         break;
331
332                 case 'T':       /* only output ber's to given file */
333                         copyfname = strdup( optarg );
334 /*                      copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY); */
335                         break;
336
337                 default:
338                     ++errflg;
339                 }
340         }
341
342         if ( host == NULL && optind == argc - 1 ) {
343                 host = argv[ optind ];
344                 ++optind;
345         }
346
347         if ( errflg || optind < argc - 1 ) {
348                 fprintf( stderr, usage, argv[ 0 ] );
349                 exit( EXIT_FAILURE );
350         }
351         
352         printf( "ldap_init( %s, %d )\n",
353                 host == NULL ? "(null)" : host, port );
354
355         ld = ldap_init( host, port );
356
357         if ( ld == NULL ) {
358                 perror( "ldap_init" );
359                 exit( EXIT_FAILURE );
360         }
361
362         if ( copyfname != NULL ) {
363                 if ( ( ld->ld_sb->sb_fd = open( copyfname, O_WRONLY|O_CREAT|O_EXCL,
364                     0600 ))  == -1 ) {
365                         perror( copyfname );
366                         exit ( EXIT_FAILURE );
367                 }
368                 ld->ld_sb->sb_options = copyoptions;
369         }
370
371         bound = 0;
372         timeout.tv_sec = 0;
373         timeout.tv_usec = 0;
374
375         (void) memset( line, '\0', sizeof(line) );
376         while ( get_line( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
377                 command1 = line[0];
378                 command2 = line[1];
379                 command3 = line[2];
380
381                 switch ( command1 ) {
382                 case 'a':       /* add or abandon */
383                         switch ( command2 ) {
384                         case 'd':       /* add */
385                                 get_line( dn, sizeof(dn), stdin, "dn? " );
386                                 strcat( dn, dnsuffix );
387                                 if ( (attrs = get_modlist( NULL, "attr? ",
388                                     "value? " )) == NULL )
389                                         break;
390                                 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
391                                         ldap_perror( ld, "ldap_add" );
392                                 else
393                                         printf( "Add initiated with id %d\n",
394                                             id );
395                                 break;
396
397                         case 'b':       /* abandon */
398                                 get_line( line, sizeof(line), stdin, "msgid? " );
399                                 id = atoi( line );
400                                 if ( ldap_abandon( ld, id ) != 0 )
401                                         ldap_perror( ld, "ldap_abandon" );
402                                 else
403                                         printf( "Abandon successful\n" );
404                                 break;
405                         default:
406                                 printf( "Possibilities: [ad]d, [ab]ort\n" );
407                         }
408                         break;
409
410                 case 'b':       /* asynch bind */
411 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
412                         get_line( line, sizeof(line), stdin,
413                             "method (0->simple, 1->krbv41, 2->krbv42)? " );
414                         method = atoi( line ) | 0x80;
415 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
416                         method = LDAP_AUTH_SIMPLE;
417 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
418                         get_line( dn, sizeof(dn), stdin, "dn? " );
419                         strcat( dn, dnsuffix );
420
421                         if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
422                                 get_line( passwd, sizeof(passwd), stdin,
423                                     "password? " );
424                         else
425                                 passwd[0] = '\0';
426
427                         if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
428                                 fprintf( stderr, "ldap_bind failed\n" );
429                                 ldap_perror( ld, "ldap_bind" );
430                         } else {
431                                 printf( "Bind initiated\n" );
432                                 bound = 1;
433                         }
434                         break;
435
436                 case 'B':       /* synch bind */
437 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
438                         get_line( line, sizeof(line), stdin,
439                             "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
440                         method = atoi( line );
441                         if ( method == 3 )
442                                 method = LDAP_AUTH_KRBV4;
443                         else
444                                 method = method | 0x80;
445 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
446                         method = LDAP_AUTH_SIMPLE;
447 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
448                         get_line( dn, sizeof(dn), stdin, "dn? " );
449                         strcat( dn, dnsuffix );
450
451                         if ( dn[0] != '\0' )
452                                 get_line( passwd, sizeof(passwd), stdin,
453                                     "password? " );
454                         else
455                                 passwd[0] = '\0';
456
457                         if ( ldap_bind_s( ld, dn, passwd, method ) !=
458                             LDAP_SUCCESS ) {
459                                 fprintf( stderr, "ldap_bind_s failed\n" );
460                                 ldap_perror( ld, "ldap_bind_s" );
461                         } else {
462                                 printf( "Bind successful\n" );
463                                 bound = 1;
464                         }
465                         break;
466
467                 case 'c':       /* compare */
468                         get_line( dn, sizeof(dn), stdin, "dn? " );
469                         strcat( dn, dnsuffix );
470                         get_line( attr, sizeof(attr), stdin, "attr? " );
471                         get_line( value, sizeof(value), stdin, "value? " );
472
473                         if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
474                                 ldap_perror( ld, "ldap_compare" );
475                         else
476                                 printf( "Compare initiated with id %d\n", id );
477                         break;
478
479                 case 'd':       /* turn on debugging */
480 #ifdef LDAP_DEBUG
481                         get_line( line, sizeof(line), stdin, "debug level? " );
482                         ldap_debug = atoi( line );
483 #ifdef LBER_DEBUG
484                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
485                                 ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
486                         }
487 #endif
488 #else
489                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
490 #endif
491                         break;
492
493                 case 'E':       /* explode a dn */
494                         get_line( line, sizeof(line), stdin, "dn? " );
495                         exdn = ldap_explode_dn( line, 0 );
496                         for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
497                                 printf( "\t%s\n", exdn[i] );
498                         }
499                         break;
500
501                 case 'g':       /* set next msgid */
502                         get_line( line, sizeof(line), stdin, "msgid? " );
503                         ld->ld_msgid = atoi( line );
504                         break;
505
506                 case 'v':       /* set version number */
507                         get_line( line, sizeof(line), stdin, "version? " );
508                         ld->ld_version = atoi( line );
509                         break;
510
511                 case 'm':       /* modify or modifyrdn */
512                         if ( strncmp( line, "modify", 4 ) == 0 ) {
513                                 get_line( dn, sizeof(dn), stdin, "dn? " );
514                                 strcat( dn, dnsuffix );
515                                 if ( (mods = get_modlist(
516                                     "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
517                                     "attribute type? ", "attribute value? " ))
518                                     == NULL )
519                                         break;
520                                 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
521                                         ldap_perror( ld, "ldap_modify" );
522                                 else
523                                         printf( "Modify initiated with id %d\n",
524                                             id );
525                         } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
526                                 get_line( dn, sizeof(dn), stdin, "dn? " );
527                                 strcat( dn, dnsuffix );
528                                 get_line( rdn, sizeof(rdn), stdin, "newrdn? " );
529                                 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
530                                         ldap_perror( ld, "ldap_modrdn" );
531                                 else
532                                         printf( "Modrdn initiated with id %d\n",
533                                             id );
534                         } else {
535                                 printf( "Possibilities: [modi]fy, [modr]dn\n" );
536                         }
537                         break;
538
539                 case 'q':       /* quit */
540                         ldap_unbind( ld );
541                         exit( EXIT_SUCCESS );
542                         break;
543
544                 case 'r':       /* result or remove */
545                         switch ( command3 ) {
546                         case 's':       /* result */
547                                 get_line( line, sizeof(line), stdin,
548                                     "msgid (-1=>any)? " );
549                                 if ( line[0] == '\0' )
550                                         id = -1;
551                                 else
552                                         id = atoi( line );
553                                 get_line( line, sizeof(line), stdin,
554                                     "all (0=>any, 1=>all)? " );
555                                 if ( line[0] == '\0' )
556                                         all = 1;
557                                 else
558                                         all = atoi( line );
559                                 if (( msgtype = ldap_result( ld, id, all,
560                                     &timeout, &res )) < 1 ) {
561                                         ldap_perror( ld, "ldap_result" );
562                                         break;
563                                 }
564                                 printf( "\nresult: msgtype %d msgid %d\n",
565                                     msgtype, res->lm_msgid );
566                                 handle_result( ld, res );
567                                 res = NULL;
568                                 break;
569
570                         case 'm':       /* remove */
571                                 get_line( dn, sizeof(dn), stdin, "dn? " );
572                                 strcat( dn, dnsuffix );
573                                 if ( (id = ldap_delete( ld, dn )) == -1 )
574                                         ldap_perror( ld, "ldap_delete" );
575                                 else
576                                         printf( "Remove initiated with id %d\n",
577                                             id );
578                                 break;
579
580                         default:
581                                 printf( "Possibilities: [rem]ove, [res]ult\n" );
582                                 break;
583                         }
584                         break;
585
586                 case 's':       /* search */
587                         get_line( dn, sizeof(dn), stdin, "searchbase? " );
588                         strcat( dn, dnsuffix );
589                         get_line( line, sizeof(line), stdin,
590                             "scope (0=baseObject, 1=oneLevel, 2=subtree, 3=children)? " );
591                         scope = atoi( line );
592                         get_line( filter, sizeof(filter), stdin,
593                             "search filter (e.g. sn=jones)? " );
594                         types = get_list( "attrs to return? " );
595                         get_line( line, sizeof(line), stdin,
596                             "attrsonly (0=attrs&values, 1=attrs only)? " );
597                         attrsonly = atoi( line );
598
599                             if (( id = ldap_search( ld, dn, scope, filter,
600                                     types, attrsonly  )) == -1 ) {
601                                 ldap_perror( ld, "ldap_search" );
602                             } else {
603                                 printf( "Search initiated with id %d\n", id );
604                             }
605                         free_list( types );
606                         break;
607
608                 case 't':       /* set timeout value */
609                         get_line( line, sizeof(line), stdin, "timeout? " );
610                         timeout.tv_sec = atoi( line );
611                         break;
612
613                 case 'p':       /* parse LDAP URL */
614                         get_line( line, sizeof(line), stdin, "LDAP URL? " );
615                         if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
616                             fprintf( stderr, "ldap_url_parse: error %d\n", i );
617                         } else {
618                             printf( "\t  host: " );
619                             if ( ludp->lud_host == NULL ) {
620                                 printf( "DEFAULT\n" );
621                             } else {
622                                 printf( "<%s>\n", ludp->lud_host );
623                             }
624                             printf( "\t  port: " );
625                             if ( ludp->lud_port == 0 ) {
626                                 printf( "DEFAULT\n" );
627                             } else {
628                                 printf( "%d\n", ludp->lud_port );
629                             }
630                             printf( "\t    dn: <%s>\n", ludp->lud_dn );
631                             printf( "\t attrs:" );
632                             if ( ludp->lud_attrs == NULL ) {
633                                 printf( " ALL" );
634                             } else {
635                                 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
636                                     printf( " <%s>", ludp->lud_attrs[ i ] );
637                                 }
638                             }
639                             printf( "\n\t scope: %s\n",
640                                         ludp->lud_scope == LDAP_SCOPE_BASE ? "baseObject"
641                                         : ludp->lud_scope == LDAP_SCOPE_ONELEVEL ? "oneLevel"
642                                         : ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "subtree"
643 #ifdef LDAP_SCOPE_SUBORDINATE
644                                         : ludp->lud_scope == LDAP_SCOPE_SUBORDINATE ? "children"
645 #endif
646                                         : "**invalid**" );
647                             printf( "\tfilter: <%s>\n", ludp->lud_filter );
648                             ldap_free_urldesc( ludp );
649                         }
650                             break;
651
652                 case 'n':       /* set dn suffix, for convenience */
653                         get_line( line, sizeof(line), stdin, "DN suffix? " );
654                         strcpy( dnsuffix, line );
655                         break;
656
657                 case 'o':       /* set ldap options */
658                         get_line( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
659                         ld->ld_deref = atoi( line );
660                         get_line( line, sizeof(line), stdin, "timelimit?" );
661                         ld->ld_timelimit = atoi( line );
662                         get_line( line, sizeof(line), stdin, "sizelimit?" );
663                         ld->ld_sizelimit = atoi( line );
664
665                         LDAP_BOOL_ZERO(&ld->ld_options);
666
667                         get_line( line, sizeof(line), stdin,
668                                 "Recognize and chase referrals (0=no, 1=yes)?" );
669                         if ( atoi( line ) != 0 ) {
670                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
671                                 get_line( line, sizeof(line), stdin,
672                                         "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
673                                 if ( atoi( line ) != 0 ) {
674                                         ldap_set_rebind_proc( ld, bind_prompt, NULL );
675                                 }
676                         }
677                         break;
678
679                 case '?':       /* help */
680                         printf(
681 "Commands: [ad]d         [ab]andon         [b]ind\n"
682 "          [B]ind async  [c]ompare\n"
683 "          [modi]fy      [modr]dn          [rem]ove\n"
684 "          [res]ult      [s]earch          [q]uit/unbind\n\n"
685 "          [d]ebug       set ms[g]id\n"
686 "          d[n]suffix    [t]imeout         [v]ersion\n"
687 "          [?]help       [o]ptions"
688 "          [E]xplode dn  [p]arse LDAP URL\n" );
689                         break;
690
691                 default:
692                         printf( "Invalid command.  Type ? for help.\n" );
693                         break;
694                 }
695
696                 (void) memset( line, '\0', sizeof(line) );
697         }
698
699         return( 0 );
700 }
701
702 static void
703 handle_result( LDAP *ld, LDAPMessage *lm )
704 {
705         switch ( lm->lm_msgtype ) {
706         case LDAP_RES_COMPARE:
707                 printf( "Compare result\n" );
708                 print_ldap_result( ld, lm, "compare" );
709                 break;
710
711         case LDAP_RES_SEARCH_RESULT:
712                 printf( "Search result\n" );
713                 print_ldap_result( ld, lm, "search" );
714                 break;
715
716         case LDAP_RES_SEARCH_ENTRY:
717                 printf( "Search entry\n" );
718                 print_search_entry( ld, lm );
719                 break;
720
721         case LDAP_RES_ADD:
722                 printf( "Add result\n" );
723                 print_ldap_result( ld, lm, "add" );
724                 break;
725
726         case LDAP_RES_DELETE:
727                 printf( "Delete result\n" );
728                 print_ldap_result( ld, lm, "delete" );
729                 break;
730
731         case LDAP_RES_MODRDN:
732                 printf( "ModRDN result\n" );
733                 print_ldap_result( ld, lm, "modrdn" );
734                 break;
735
736         case LDAP_RES_BIND:
737                 printf( "Bind result\n" );
738                 print_ldap_result( ld, lm, "bind" );
739                 break;
740
741         default:
742                 printf( "Unknown result type 0x%lx\n",
743                         (unsigned long) lm->lm_msgtype );
744                 print_ldap_result( ld, lm, "unknown" );
745         }
746 }
747
748 static void
749 print_ldap_result( LDAP *ld, LDAPMessage *lm, const char *s )
750 {
751         ldap_result2error( ld, lm, 1 );
752         ldap_perror( ld, s );
753 /*
754         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
755                 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
756         if ( LDAP_NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
757                 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
758 */
759 }
760
761 static void
762 print_search_entry( LDAP *ld, LDAPMessage *res )
763 {
764         LDAPMessage     *e;
765
766         for ( e = ldap_first_entry( ld, res ); e != NULL;
767             e = ldap_next_entry( ld, e ) )
768         {
769                 BerElement      *ber = NULL;
770                 char *a, *dn, *ufn;
771
772                 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
773                         break;
774
775                 dn = ldap_get_dn( ld, e );
776                 printf( "\tDN: %s\n", dn );
777
778                 ufn = ldap_dn2ufn( dn );
779                 printf( "\tUFN: %s\n", ufn );
780
781                 free( dn );
782                 free( ufn );
783
784                 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
785                     a = ldap_next_attribute( ld, e, ber ) )
786                 {
787                         struct berval   **vals;
788
789                         printf( "\t\tATTR: %s\n", a );
790                         if ( (vals = ldap_get_values_len( ld, e, a ))
791                             == NULL ) {
792                                 printf( "\t\t\t(no values)\n" );
793                         } else {
794                                 int i;
795                                 for ( i = 0; vals[i] != NULL; i++ ) {
796                                         int     j, nonascii;
797
798                                         nonascii = 0;
799                                         for ( j = 0; (ber_len_t) j < vals[i]->bv_len; j++ )
800                                                 if ( !isascii( vals[i]->bv_val[j] ) ) {
801                                                         nonascii = 1;
802                                                         break;
803                                                 }
804
805                                         if ( nonascii ) {
806                                                 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
807 #ifdef BPRINT_NONASCII
808                                                 ber_bprint( vals[i]->bv_val,
809                                                     vals[i]->bv_len );
810 #endif /* BPRINT_NONASCII */
811                                                 continue;
812                                         }
813                                         printf( "\t\t\tlength (%ld) %s\n",
814                                             vals[i]->bv_len, vals[i]->bv_val );
815                                 }
816                                 ber_bvecfree( vals );
817                         }
818                 }
819
820                 if(ber != NULL) {
821                         ber_free( ber, 0 );
822                 }
823         }
824
825         if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
826             || res->lm_chain != NULL )
827                 print_ldap_result( ld, res, "search" );
828 }