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