]> git.sur5r.net Git - openldap/blob - libraries/libldap/test.c
Add libtool support based upon patch by Bart Hartgers <Hartgers@kfm1.phys.tue.nl>
[openldap] / libraries / libldap / test.c
1 #include "portable.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <ac/ctype.h>
7 #include <ac/socket.h>
8 #include <ac/string.h>
9 #include <ac/time.h>
10 #include <ac/unistd.h>
11
12 #include <sys/stat.h>
13
14 #ifdef HAVE_SYS_FILE_H
15 #include <sys/file.h>
16 #endif
17 #ifdef HAVE_IO_H
18 #include <io.h>
19 #endif
20
21 #include <fcntl.h>
22
23 /* including the "internal" defs is legit and nec. since this test routine has 
24  * a-priori knowledge of libldap internal workings.
25  * hodges@stanford.edu 5-Feb-96
26  */
27 #include "ldap-int.h"
28
29 /* local functions */
30 #ifndef HAVE_GETLINE
31 static char *getline LDAP_P(( char *line, int len, FILE *fp, char *prompt ));
32 #endif
33 static char **get_list LDAP_P(( char *prompt ));
34 static int file_read LDAP_P(( char *path, struct berval *bv ));
35 static LDAPMod **get_modlist LDAP_P(( char *prompt1, char *prompt2, char *prompt3 ));
36 static void handle_result LDAP_P(( LDAP *ld, LDAPMessage *lm ));
37 static void print_ldap_result LDAP_P(( LDAP *ld, LDAPMessage *lm, char *s ));
38 static void print_search_entry LDAP_P(( LDAP *ld, LDAPMessage *res ));
39 static void free_list LDAP_P(( char **list ));
40
41 #define NOCACHEERRMSG   "don't compile with -DLDAP_NOCACHE if you desire local caching"
42
43 static char *dnsuffix;
44
45 #ifndef HAVE_GETLINE
46 static char *
47 getline( char *line, int len, FILE *fp, char *prompt )
48 {
49         printf(prompt);
50
51         if ( fgets( line, len, fp ) == NULL )
52                 return( NULL );
53
54         line[ strlen( line ) - 1 ] = '\0';
55
56         return( line );
57 }
58 #endif
59
60 static char **
61 get_list( char *prompt )
62 {
63         static char     buf[256];
64         int             num;
65         char            **result;
66
67         num = 0;
68         result = (char **) 0;
69         while ( 1 ) {
70                 getline( buf, sizeof(buf), stdin, prompt );
71
72                 if ( *buf == '\0' )
73                         break;
74
75                 if ( result == (char **) 0 )
76                         result = (char **) malloc( sizeof(char *) );
77                 else
78                         result = (char **) realloc( result,
79                             sizeof(char *) * (num + 1) );
80
81                 result[num++] = (char *) ldap_strdup( buf );
82         }
83         if ( result == (char **) 0 )
84                 return( NULL );
85         result = (char **) realloc( result, sizeof(char *) * (num + 1) );
86         result[num] = NULL;
87
88         return( result );
89 }
90
91
92 static void
93 free_list( char **list )
94 {
95         int     i;
96
97         if ( list != NULL ) {
98                 for ( i = 0; list[ i ] != NULL; ++i ) {
99                         free( list[ i ] );
100                 }
101                 free( (char *)list );
102         }
103 }
104
105
106 static int
107 file_read( char *path, struct berval *bv )
108 {
109         FILE            *fp;
110         long            rlen;
111         int             eof;
112
113         if (( fp = fopen( path, "r" )) == NULL ) {
114                 perror( path );
115                 return( -1 );
116         }
117
118         if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
119                 perror( path );
120                 fclose( fp );
121                 return( -1 );
122         }
123
124         bv->bv_len = ftell( fp );
125
126         if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
127                 perror( "malloc" );
128                 fclose( fp );
129                 return( -1 );
130         }
131
132         if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
133                 perror( path );
134                 fclose( fp );
135                 return( -1 );
136         }
137
138         rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
139         eof = feof( fp );
140         fclose( fp );
141
142         if ( (unsigned long) rlen != bv->bv_len ) {
143                 perror( path );
144                 free( bv->bv_val );
145                 return( -1 );
146         }
147
148         return( bv->bv_len );
149 }
150
151
152 static LDAPMod **
153 get_modlist( char *prompt1, char *prompt2, char *prompt3 )
154 {
155         static char     buf[256];
156         int             num;
157         LDAPMod         tmp;
158         LDAPMod         **result;
159         struct berval   **bvals;
160
161         num = 0;
162         result = NULL;
163         while ( 1 ) {
164                 if ( prompt1 ) {
165                         getline( buf, sizeof(buf), stdin, prompt1 );
166                         tmp.mod_op = atoi( buf );
167
168                         if ( tmp.mod_op == -1 || buf[0] == '\0' )
169                                 break;
170                 }
171
172                 getline( buf, sizeof(buf), stdin, prompt2 );
173                 if ( buf[0] == '\0' )
174                         break;
175                 tmp.mod_type = ldap_strdup( buf );
176
177                 tmp.mod_values = get_list( prompt3 );
178
179                 if ( tmp.mod_values != NULL ) {
180                         int     i;
181
182                         for ( i = 0; tmp.mod_values[i] != NULL; ++i )
183                                 ;
184                         bvals = (struct berval **)calloc( i + 1,
185                             sizeof( struct berval *));
186                         for ( i = 0; tmp.mod_values[i] != NULL; ++i ) {
187                                 bvals[i] = (struct berval *)malloc(
188                                     sizeof( struct berval ));
189                                 if ( strncmp( tmp.mod_values[i], "{FILE}",
190                                     6 ) == 0 ) {
191                                         if ( file_read( tmp.mod_values[i] + 6,
192                                             bvals[i] ) < 0 ) {
193                                                 return( NULL );
194                                         }
195                                 } else {
196                                         bvals[i]->bv_val = tmp.mod_values[i];
197                                         bvals[i]->bv_len =
198                                             strlen( tmp.mod_values[i] );
199                                 }
200                         }
201                         tmp.mod_bvalues = bvals;
202                         tmp.mod_op |= LDAP_MOD_BVALUES;
203                 }
204
205                 if ( result == NULL )
206                         result = (LDAPMod **) malloc( sizeof(LDAPMod *) );
207                 else
208                         result = (LDAPMod **) realloc( result,
209                             sizeof(LDAPMod *) * (num + 1) );
210
211                 result[num] = (LDAPMod *) malloc( sizeof(LDAPMod) );
212                 *(result[num]) = tmp;   /* struct copy */
213                 num++;
214         }
215         if ( result == NULL )
216                 return( NULL );
217         result = (LDAPMod **) realloc( result, sizeof(LDAPMod *) * (num + 1) );
218         result[num] = NULL;
219
220         return( result );
221 }
222
223
224 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
225 static int
226 bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
227         int freeit )
228 {
229         static char     dn[256], passwd[256];
230
231         if ( !freeit ) {
232 #ifdef HAVE_KERBEROS
233                 getline( dn, sizeof(dn), stdin,
234                     "re-bind method (0->simple, 1->krbv41, 2->krbv42, 3->krbv41&2)? " );
235                 if (( *authmethodp = atoi( dn )) == 3 ) {
236                         *authmethodp = LDAP_AUTH_KRBV4;
237                 } else {
238                         *authmethodp |= 0x80;
239                 }
240 #else /* HAVE_KERBEROS */
241                 *authmethodp = LDAP_AUTH_SIMPLE;
242 #endif /* HAVE_KERBEROS */
243
244                 getline( dn, sizeof(dn), stdin, "re-bind dn? " );
245                 strcat( dn, dnsuffix );
246                 *dnp = dn;
247
248                 if ( *authmethodp == LDAP_AUTH_SIMPLE && dn[0] != '\0' ) {
249                         getline( passwd, sizeof(passwd), stdin,
250                             "re-bind password? " );
251                 } else {
252                         passwd[0] = '\0';
253                 }
254                 *passwdp = passwd;
255         }
256
257         return( LDAP_SUCCESS );
258 }
259 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
260
261
262 int
263 main( int argc, char **argv )
264 {
265         LDAP            *ld = NULL;
266         int             i, c, port, cldapflg, errflg, method, id, msgtype;
267         char            line[256], command1, command2, command3;
268         char            passwd[64], dn[256], rdn[64], attr[64], value[256];
269         char            filter[256], *host, **types;
270         char            **exdn;
271         char            *usage = "usage: %s [-u] [-h host] [-d level] [-s dnsuffix] [-p port] [-t file] [-T file]\n";
272         int             bound, all, scope, attrsonly;
273         LDAPMessage     *res;
274         LDAPMod         **mods, **attrs;
275         struct timeval  timeout;
276         char            *copyfname = NULL;
277         int             copyoptions = 0;
278         LDAPURLDesc     *ludp;
279
280         host = NULL;
281         port = LDAP_PORT;
282         dnsuffix = "";
283         cldapflg = errflg = 0;
284
285         while (( c = getopt( argc, argv, "uh:d:s:p:t:T:" )) != -1 ) {
286                 switch( c ) {
287                 case 'u':
288 #ifdef LDAP_CONNECTIONLESS
289                         cldapflg++;
290 #else /* LDAP_CONNECTIONLESS */
291                         printf( "Compile with -DLDAP_CONNECTIONLESS for UDP support\n" );
292 #endif /* LDAP_CONNECTIONLESS */
293                         break;
294
295                 case 'd':
296 #ifdef LDAP_DEBUG
297                         ldap_debug = atoi( optarg );
298                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
299                                 lber_debug = ldap_debug;
300                         }
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 = ldap_strdup( optarg );
320                         copyoptions = LBER_TO_FILE;
321                         break;
322
323                 case 'T':       /* only output ber's to given file */
324                         copyfname = ldap_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( 1 );
341         }
342         
343         printf( "%sldap_open( %s, %d )\n", cldapflg ? "c" : "",
344                 host == NULL ? "(null)" : host, port );
345
346         if ( cldapflg ) {
347 #ifdef LDAP_CONNECTIONLESS
348                 ld = cldap_open( host, port );
349 #endif /* LDAP_CONNECTIONLESS */
350         } else {
351                 ld = ldap_open( host, port );
352         }
353
354         if ( ld == NULL ) {
355                 perror( "ldap_open" );
356                 exit(1);
357         }
358
359         if ( copyfname != NULL ) {
360                 if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
361                     0600 ))  == -1 ) {
362                         perror( copyfname );
363                         exit ( 1 );
364                 }
365                 ld->ld_sb.sb_options = copyoptions;
366         }
367
368         bound = 0;
369         timeout.tv_sec = 0;
370         timeout.tv_usec = 0;
371
372         (void) memset( line, '\0', sizeof(line) );
373         while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
374                 command1 = line[0];
375                 command2 = line[1];
376                 command3 = line[2];
377
378                 switch ( command1 ) {
379                 case 'a':       /* add or abandon */
380                         switch ( command2 ) {
381                         case 'd':       /* add */
382                                 getline( dn, sizeof(dn), stdin, "dn? " );
383                                 strcat( dn, dnsuffix );
384                                 if ( (attrs = get_modlist( NULL, "attr? ",
385                                     "value? " )) == NULL )
386                                         break;
387                                 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
388                                         ldap_perror( ld, "ldap_add" );
389                                 else
390                                         printf( "Add initiated with id %d\n",
391                                             id );
392                                 break;
393
394                         case 'b':       /* abandon */
395                                 getline( line, sizeof(line), stdin, "msgid? " );
396                                 id = atoi( line );
397                                 if ( ldap_abandon( ld, id ) != 0 )
398                                         ldap_perror( ld, "ldap_abandon" );
399                                 else
400                                         printf( "Abandon successful\n" );
401                                 break;
402                         default:
403                                 printf( "Possibilities: [ad]d, [ab]ort\n" );
404                         }
405                         break;
406
407                 case 'b':       /* asynch bind */
408 #ifdef HAVE_KERBEROS
409                         getline( line, sizeof(line), stdin,
410                             "method (0->simple, 1->krbv41, 2->krbv42)? " );
411                         method = atoi( line ) | 0x80;
412 #else /* HAVE_KERBEROS */
413                         method = LDAP_AUTH_SIMPLE;
414 #endif /* HAVE_KERBEROS */
415                         getline( dn, sizeof(dn), stdin, "dn? " );
416                         strcat( dn, dnsuffix );
417
418                         if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
419                                 getline( passwd, sizeof(passwd), stdin,
420                                     "password? " );
421                         else
422                                 passwd[0] = '\0';
423
424                         if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
425                                 fprintf( stderr, "ldap_bind failed\n" );
426                                 ldap_perror( ld, "ldap_bind" );
427                         } else {
428                                 printf( "Bind initiated\n" );
429                                 bound = 1;
430                         }
431                         break;
432
433                 case 'B':       /* synch bind */
434 #ifdef HAVE_KERBEROS
435                         getline( line, sizeof(line), stdin,
436                             "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
437                         method = atoi( line );
438                         if ( method == 3 )
439                                 method = LDAP_AUTH_KRBV4;
440                         else
441                                 method = method | 0x80;
442 #else /* HAVE_KERBEROS */
443                         method = LDAP_AUTH_SIMPLE;
444 #endif /* HAVE_KERBEROS */
445                         getline( dn, sizeof(dn), stdin, "dn? " );
446                         strcat( dn, dnsuffix );
447
448                         if ( dn[0] != '\0' )
449                                 getline( passwd, sizeof(passwd), stdin,
450                                     "password? " );
451                         else
452                                 passwd[0] = '\0';
453
454                         if ( ldap_bind_s( ld, dn, passwd, method ) !=
455                             LDAP_SUCCESS ) {
456                                 fprintf( stderr, "ldap_bind_s failed\n" );
457                                 ldap_perror( ld, "ldap_bind_s" );
458                         } else {
459                                 printf( "Bind successful\n" );
460                                 bound = 1;
461                         }
462                         break;
463
464                 case 'c':       /* compare */
465                         getline( dn, sizeof(dn), stdin, "dn? " );
466                         strcat( dn, dnsuffix );
467                         getline( attr, sizeof(attr), stdin, "attr? " );
468                         getline( value, sizeof(value), stdin, "value? " );
469
470                         if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
471                                 ldap_perror( ld, "ldap_compare" );
472                         else
473                                 printf( "Compare initiated with id %d\n", id );
474                         break;
475
476                 case 'd':       /* turn on debugging */
477 #ifdef LDAP_DEBUG
478                         getline( line, sizeof(line), stdin, "debug level? " );
479                         ldap_debug = atoi( line );
480                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
481                                 lber_debug = ldap_debug;
482                         }
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 #ifdef LDAP_CONNECTIONLESS
536                         if ( cldapflg )
537                                 cldap_close( ld );
538 #endif /* LDAP_CONNECTIONLESS */
539 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
540                         if ( !cldapflg )
541 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
542                         if ( !cldapflg && bound )
543 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
544                                 ldap_unbind( ld );
545                         exit( 0 );
546                         break;
547
548                 case 'r':       /* result or remove */
549                         switch ( command3 ) {
550                         case 's':       /* result */
551                                 getline( line, sizeof(line), stdin,
552                                     "msgid (-1=>any)? " );
553                                 if ( line[0] == '\0' )
554                                         id = -1;
555                                 else
556                                         id = atoi( line );
557                                 getline( line, sizeof(line), stdin,
558                                     "all (0=>any, 1=>all)? " );
559                                 if ( line[0] == '\0' )
560                                         all = 1;
561                                 else
562                                         all = atoi( line );
563                                 if (( msgtype = ldap_result( ld, id, all,
564                                     &timeout, &res )) < 1 ) {
565                                         ldap_perror( ld, "ldap_result" );
566                                         break;
567                                 }
568                                 printf( "\nresult: msgtype %d msgid %d\n",
569                                     msgtype, res->lm_msgid );
570                                 handle_result( ld, res );
571                                 res = NULLMSG;
572                                 break;
573
574                         case 'm':       /* remove */
575                                 getline( dn, sizeof(dn), stdin, "dn? " );
576                                 strcat( dn, dnsuffix );
577                                 if ( (id = ldap_delete( ld, dn )) == -1 )
578                                         ldap_perror( ld, "ldap_delete" );
579                                 else
580                                         printf( "Remove initiated with id %d\n",
581                                             id );
582                                 break;
583
584                         default:
585                                 printf( "Possibilities: [rem]ove, [res]ult\n" );
586                                 break;
587                         }
588                         break;
589
590                 case 's':       /* search */
591                         getline( dn, sizeof(dn), stdin, "searchbase? " );
592                         strcat( dn, dnsuffix );
593                         getline( line, sizeof(line), stdin,
594                             "scope (0=Base, 1=One Level, 2=Subtree)? " );
595                         scope = atoi( line );
596                         getline( filter, sizeof(filter), stdin,
597                             "search filter (e.g. sn=jones)? " );
598                         types = get_list( "attrs to return? " );
599                         getline( line, sizeof(line), stdin,
600                             "attrsonly (0=attrs&values, 1=attrs only)? " );
601                         attrsonly = atoi( line );
602
603                         if ( cldapflg ) {
604 #ifdef LDAP_CONNECTIONLESS
605                             getline( line, sizeof(line), stdin,
606                                 "Requestor DN (for logging)? " );
607                             if ( cldap_search_s( ld, dn, scope, filter, types,
608                                     attrsonly, &res, line ) != 0 ) {
609                                 ldap_perror( ld, "cldap_search_s" );
610                             } else {
611                                 printf( "\nresult: msgid %d\n",
612                                     res->lm_msgid );
613                                 handle_result( ld, res );
614                                 res = NULLMSG;
615                             }
616 #endif /* LDAP_CONNECTIONLESS */
617                         } else {
618                             if (( id = ldap_search( ld, dn, scope, filter,
619                                     types, attrsonly  )) == -1 ) {
620                                 ldap_perror( ld, "ldap_search" );
621                             } else {
622                                 printf( "Search initiated with id %d\n", id );
623                             }
624                         }
625                         free_list( types );
626                         break;
627
628                 case 't':       /* set timeout value */
629                         getline( line, sizeof(line), stdin, "timeout? " );
630                         timeout.tv_sec = atoi( line );
631                         break;
632
633                 case 'U':       /* set ufn search prefix */
634                         getline( line, sizeof(line), stdin, "ufn prefix? " );
635                         ldap_ufn_setprefix( ld, line );
636                         break;
637
638                 case 'u':       /* user friendly search w/optional timeout */
639                         getline( dn, sizeof(dn), stdin, "ufn? " );
640                         strcat( dn, dnsuffix );
641                         types = get_list( "attrs to return? " );
642                         getline( line, sizeof(line), stdin,
643                             "attrsonly (0=attrs&values, 1=attrs only)? " );
644                         attrsonly = atoi( line );
645
646                         if ( command2 == 't' ) {
647                                 id = ldap_ufn_search_c( ld, dn, types,
648                                     attrsonly, &res, ldap_ufn_timeout,
649                                     &timeout );
650                         } else {
651                                 id = ldap_ufn_search_s( ld, dn, types,
652                                     attrsonly, &res );
653                         }
654                         if ( res == NULL )
655                                 ldap_perror( ld, "ldap_ufn_search" );
656                         else {
657                                 printf( "\nresult: err %d\n", id );
658                                 handle_result( ld, res );
659                                 res = NULLMSG;
660                         }
661                         free_list( types );
662                         break;
663
664                 case 'l':       /* URL search */
665                         getline( line, sizeof(line), stdin,
666                             "attrsonly (0=attrs&values, 1=attrs only)? " );
667                         attrsonly = atoi( line );
668                         getline( line, sizeof(line), stdin, "LDAP URL? " );
669                         if (( id = ldap_url_search( ld, line, attrsonly  ))
670                                 == -1 ) {
671                             ldap_perror( ld, "ldap_url_search" );
672                         } else {
673                             printf( "URL search initiated with id %d\n", id );
674                         }
675                         break;
676
677                 case 'p':       /* parse LDAP URL */
678                         getline( line, sizeof(line), stdin, "LDAP URL? " );
679                         if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
680                             fprintf( stderr, "ldap_url_parse: error %d\n", i );
681                         } else {
682                             printf( "\t  host: " );
683                             if ( ludp->lud_host == NULL ) {
684                                 printf( "DEFAULT\n" );
685                             } else {
686                                 printf( "<%s>\n", ludp->lud_host );
687                             }
688                             printf( "\t  port: " );
689                             if ( ludp->lud_port == 0 ) {
690                                 printf( "DEFAULT\n" );
691                             } else {
692                                 printf( "%d\n", ludp->lud_port );
693                             }
694                             printf( "\t    dn: <%s>\n", ludp->lud_dn );
695                             printf( "\t attrs:" );
696                             if ( ludp->lud_attrs == NULL ) {
697                                 printf( " ALL" );
698                             } else {
699                                 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
700                                     printf( " <%s>", ludp->lud_attrs[ i ] );
701                                 }
702                             }
703                             printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
704                                 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
705                                 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
706                             printf( "\tfilter: <%s>\n", ludp->lud_filter );
707                             ldap_free_urldesc( ludp );
708                         }
709                             break;
710
711                 case 'n':       /* set dn suffix, for convenience */
712                         getline( line, sizeof(line), stdin, "DN suffix? " );
713                         strcpy( dnsuffix, line );
714                         break;
715
716                 case 'e':       /* enable cache */
717 #ifdef LDAP_NOCACHE
718                         printf( NOCACHEERRMSG );
719 #else /* LDAP_NOCACHE */
720                         getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
721                         i = atoi( line );
722                         getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
723                         if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
724                                 printf( "local cache is on\n" ); 
725                         } else {
726                                 printf( "ldap_enable_cache failed\n" ); 
727                         }
728 #endif /* LDAP_NOCACHE */
729                         break;
730
731                 case 'x':       /* uncache entry */
732 #ifdef LDAP_NOCACHE
733                         printf( NOCACHEERRMSG );
734 #else /* LDAP_NOCACHE */
735                         getline( line, sizeof(line), stdin, "DN? " );
736                         ldap_uncache_entry( ld, line );
737 #endif /* LDAP_NOCACHE */
738                         break;
739
740                 case 'X':       /* uncache request */
741 #ifdef LDAP_NOCACHE
742                         printf( NOCACHEERRMSG );
743 #else /* LDAP_NOCACHE */
744                         getline( line, sizeof(line), stdin, "request msgid? " );
745                         ldap_uncache_request( ld, atoi( line ));
746 #endif /* LDAP_NOCACHE */
747                         break;
748
749                 case 'o':       /* set ldap options */
750                         getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
751                         ld->ld_deref = atoi( line );
752                         getline( line, sizeof(line), stdin, "timelimit?" );
753                         ld->ld_timelimit = atoi( line );
754                         getline( line, sizeof(line), stdin, "sizelimit?" );
755                         ld->ld_sizelimit = atoi( line );
756
757                         LDAP_BOOL_ZERO(&ld->ld_options);
758
759 #ifdef STR_TRANSLATION
760                         getline( line, sizeof(line), stdin,
761                                 "Automatic translation of T.61 strings (0=no, 1=yes)?" );
762                         if ( atoi( line ) == 0 ) {
763                                 ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
764                         } else {
765                                 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
766 #ifdef LDAP_CHARSET_8859
767                                 getline( line, sizeof(line), stdin,
768                                         "Translate to/from ISO-8859 (0=no, 1=yes?" );
769                                 if ( atoi( line ) != 0 ) {
770                                         ldap_set_string_translators( ld,
771                                             ldap_8859_to_t61,
772                                             ldap_t61_to_8859 );
773                                 }
774 #endif /* LDAP_CHARSET_8859 */
775                         }
776 #endif /* STR_TRANSLATION */
777
778 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
779                         getline( line, sizeof(line), stdin,
780                                 "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
781                         if ( atoi( line ) != 0 ) {
782                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS);
783                         }
784 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
785
786 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
787                         getline( line, sizeof(line), stdin,
788                                 "Recognize and chase referrals (0=no, 1=yes)?" );
789                         if ( atoi( line ) != 0 ) {
790                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
791                                 getline( line, sizeof(line), stdin,
792                                         "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
793                                 if ( atoi( line ) != 0 ) {
794                                         ldap_set_rebind_proc( ld, bind_prompt );
795                                 }
796                         }
797 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
798                         break;
799
800                 case 'O':       /* set cache options */
801 #ifdef LDAP_NOCACHE
802                         printf( NOCACHEERRMSG );
803 #else /* LDAP_NOCACHE */
804                         getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
805                         switch( atoi( line )) {
806                         case 0:
807                                 ldap_set_cache_options( ld, 0 );
808                                 break;
809                         case 1:
810                                 ldap_set_cache_options( ld,
811                                         LDAP_CACHE_OPT_CACHENOERRS );
812                                 break;
813                         case 2:
814                                 ldap_set_cache_options( ld,
815                                         LDAP_CACHE_OPT_CACHEALLERRS );
816                                 break;
817                         default:
818                                 printf( "not a valid cache option\n" );
819                         }
820 #endif /* LDAP_NOCACHE */
821                         break;
822
823                 case '?':       /* help */
824     printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
825     printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
826     printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
827     printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
828     printf( "          [u]fn search  [ut]fn search with timeout\n" );
829     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
830     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
831     printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\n" );
832     printf( "          [?]help       [o]ptions         [O]cache options\n" );
833     printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
834                         break;
835
836                 default:
837                         printf( "Invalid command.  Type ? for help.\n" );
838                         break;
839                 }
840
841                 (void) memset( line, '\0', sizeof(line) );
842         }
843
844         return( 0 );
845 }
846
847 static void
848 handle_result( LDAP *ld, LDAPMessage *lm )
849 {
850         switch ( lm->lm_msgtype ) {
851         case LDAP_RES_COMPARE:
852                 printf( "Compare result\n" );
853                 print_ldap_result( ld, lm, "compare" );
854                 break;
855
856         case LDAP_RES_SEARCH_RESULT:
857                 printf( "Search result\n" );
858                 print_ldap_result( ld, lm, "search" );
859                 break;
860
861         case LDAP_RES_SEARCH_ENTRY:
862                 printf( "Search entry\n" );
863                 print_search_entry( ld, lm );
864                 break;
865
866         case LDAP_RES_ADD:
867                 printf( "Add result\n" );
868                 print_ldap_result( ld, lm, "add" );
869                 break;
870
871         case LDAP_RES_DELETE:
872                 printf( "Delete result\n" );
873                 print_ldap_result( ld, lm, "delete" );
874                 break;
875
876         case LDAP_RES_MODRDN:
877                 printf( "ModRDN result\n" );
878                 print_ldap_result( ld, lm, "modrdn" );
879                 break;
880
881         case LDAP_RES_BIND:
882                 printf( "Bind result\n" );
883                 print_ldap_result( ld, lm, "bind" );
884                 break;
885
886         default:
887                 printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
888                 print_ldap_result( ld, lm, "unknown" );
889         }
890 }
891
892 static void
893 print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
894 {
895         ldap_result2error( ld, lm, 1 );
896         ldap_perror( ld, s );
897 /*
898         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
899                 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
900         if ( NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
901                 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
902 */
903 }
904
905 static void
906 print_search_entry( LDAP *ld, LDAPMessage *res )
907 {
908         BerElement      *ber;
909         char            *a, *dn, *ufn;
910         struct berval   **vals;
911         int             i;
912         LDAPMessage     *e;
913
914         for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
915             e = ldap_next_entry( ld, e ) ) {
916                 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
917                         break;
918
919                 dn = ldap_get_dn( ld, e );
920                 printf( "\tDN: %s\n", dn );
921
922                 ufn = ldap_dn2ufn( dn );
923                 printf( "\tUFN: %s\n", ufn );
924
925                 free( dn );
926                 free( ufn );
927
928                 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
929                     a = ldap_next_attribute( ld, e, ber ) ) {
930                         printf( "\t\tATTR: %s\n", a );
931                         if ( (vals = ldap_get_values_len( ld, e, a ))
932                             == NULL ) {
933                                 printf( "\t\t\t(no values)\n" );
934                         } else {
935                                 for ( i = 0; vals[i] != NULL; i++ ) {
936                                         int     j, nonascii;
937
938                                         nonascii = 0;
939                                         for ( j = 0; (unsigned long) j < vals[i]->bv_len; j++ )
940                                                 if ( !isascii( vals[i]->bv_val[j] ) ) {
941                                                         nonascii = 1;
942                                                         break;
943                                                 }
944
945                                         if ( nonascii ) {
946                                                 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
947 #ifdef BPRINT_NONASCII
948                                                 lber_bprint( vals[i]->bv_val,
949                                                     vals[i]->bv_len );
950 #endif /* BPRINT_NONASCII */
951                                                 continue;
952                                         }
953                                         printf( "\t\t\tlength (%ld) %s\n",
954                                             vals[i]->bv_len, vals[i]->bv_val );
955                                 }
956                                 ber_bvecfree( vals );
957                         }
958                 }
959         }
960
961         if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
962             || res->lm_chain != NULLMSG )
963                 print_ldap_result( ld, res, "search" );
964 }