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