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