]> git.sur5r.net Git - openldap/blob - libraries/libldap/test.c
Move LDAPServer, LDAPConn, LDAPRequest, and LDAPCache
[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 #ifdef LBER_DEBUG
299                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
300                                 lber_debug = ldap_debug;
301                         }
302 #endif
303 #else
304                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
305 #endif
306                         break;
307
308                 case 'h':
309                         host = optarg;
310                         break;
311
312                 case 's':
313                         dnsuffix = optarg;
314                         break;
315
316                 case 'p':
317                         port = atoi( optarg );
318                         break;
319
320                 case 't':       /* copy ber's to given file */
321                         copyfname = ldap_strdup( optarg );
322                         copyoptions = LBER_TO_FILE;
323                         break;
324
325                 case 'T':       /* only output ber's to given file */
326                         copyfname = ldap_strdup( optarg );
327                         copyoptions = (LBER_TO_FILE | LBER_TO_FILE_ONLY);
328                         break;
329
330                 default:
331                     ++errflg;
332                 }
333         }
334
335         if ( host == NULL && optind == argc - 1 ) {
336                 host = argv[ optind ];
337                 ++optind;
338         }
339
340         if ( errflg || optind < argc - 1 ) {
341                 fprintf( stderr, usage, argv[ 0 ] );
342                 exit( 1 );
343         }
344         
345         printf( "%sldap_open( %s, %d )\n", cldapflg ? "c" : "",
346                 host == NULL ? "(null)" : host, port );
347
348         if ( cldapflg ) {
349 #ifdef LDAP_CONNECTIONLESS
350                 ld = cldap_open( host, port );
351 #endif /* LDAP_CONNECTIONLESS */
352         } else {
353                 ld = ldap_open( host, port );
354         }
355
356         if ( ld == NULL ) {
357                 perror( "ldap_open" );
358                 exit(1);
359         }
360
361         if ( copyfname != NULL ) {
362                 if ( (ld->ld_sb.sb_fd = open( copyfname, O_WRONLY | O_CREAT,
363                     0600 ))  == -1 ) {
364                         perror( copyfname );
365                         exit ( 1 );
366                 }
367                 ld->ld_sb.sb_options = copyoptions;
368         }
369
370         bound = 0;
371         timeout.tv_sec = 0;
372         timeout.tv_usec = 0;
373
374         (void) memset( line, '\0', sizeof(line) );
375         while ( getline( line, sizeof(line), stdin, "\ncommand? " ) != NULL ) {
376                 command1 = line[0];
377                 command2 = line[1];
378                 command3 = line[2];
379
380                 switch ( command1 ) {
381                 case 'a':       /* add or abandon */
382                         switch ( command2 ) {
383                         case 'd':       /* add */
384                                 getline( dn, sizeof(dn), stdin, "dn? " );
385                                 strcat( dn, dnsuffix );
386                                 if ( (attrs = get_modlist( NULL, "attr? ",
387                                     "value? " )) == NULL )
388                                         break;
389                                 if ( (id = ldap_add( ld, dn, attrs )) == -1 )
390                                         ldap_perror( ld, "ldap_add" );
391                                 else
392                                         printf( "Add initiated with id %d\n",
393                                             id );
394                                 break;
395
396                         case 'b':       /* abandon */
397                                 getline( line, sizeof(line), stdin, "msgid? " );
398                                 id = atoi( line );
399                                 if ( ldap_abandon( ld, id ) != 0 )
400                                         ldap_perror( ld, "ldap_abandon" );
401                                 else
402                                         printf( "Abandon successful\n" );
403                                 break;
404                         default:
405                                 printf( "Possibilities: [ad]d, [ab]ort\n" );
406                         }
407                         break;
408
409                 case 'b':       /* asynch bind */
410 #ifdef HAVE_KERBEROS
411                         getline( line, sizeof(line), stdin,
412                             "method (0->simple, 1->krbv41, 2->krbv42)? " );
413                         method = atoi( line ) | 0x80;
414 #else /* HAVE_KERBEROS */
415                         method = LDAP_AUTH_SIMPLE;
416 #endif /* HAVE_KERBEROS */
417                         getline( dn, sizeof(dn), stdin, "dn? " );
418                         strcat( dn, dnsuffix );
419
420                         if ( method == LDAP_AUTH_SIMPLE && dn[0] != '\0' )
421                                 getline( passwd, sizeof(passwd), stdin,
422                                     "password? " );
423                         else
424                                 passwd[0] = '\0';
425
426                         if ( ldap_bind( ld, dn, passwd, method ) == -1 ) {
427                                 fprintf( stderr, "ldap_bind failed\n" );
428                                 ldap_perror( ld, "ldap_bind" );
429                         } else {
430                                 printf( "Bind initiated\n" );
431                                 bound = 1;
432                         }
433                         break;
434
435                 case 'B':       /* synch bind */
436 #ifdef HAVE_KERBEROS
437                         getline( line, sizeof(line), stdin,
438                             "method 0->simple 1->krbv41 2->krbv42 3->krb? " );
439                         method = atoi( line );
440                         if ( method == 3 )
441                                 method = LDAP_AUTH_KRBV4;
442                         else
443                                 method = method | 0x80;
444 #else /* HAVE_KERBEROS */
445                         method = LDAP_AUTH_SIMPLE;
446 #endif /* HAVE_KERBEROS */
447                         getline( dn, sizeof(dn), stdin, "dn? " );
448                         strcat( dn, dnsuffix );
449
450                         if ( dn[0] != '\0' )
451                                 getline( passwd, sizeof(passwd), stdin,
452                                     "password? " );
453                         else
454                                 passwd[0] = '\0';
455
456                         if ( ldap_bind_s( ld, dn, passwd, method ) !=
457                             LDAP_SUCCESS ) {
458                                 fprintf( stderr, "ldap_bind_s failed\n" );
459                                 ldap_perror( ld, "ldap_bind_s" );
460                         } else {
461                                 printf( "Bind successful\n" );
462                                 bound = 1;
463                         }
464                         break;
465
466                 case 'c':       /* compare */
467                         getline( dn, sizeof(dn), stdin, "dn? " );
468                         strcat( dn, dnsuffix );
469                         getline( attr, sizeof(attr), stdin, "attr? " );
470                         getline( value, sizeof(value), stdin, "value? " );
471
472                         if ( (id = ldap_compare( ld, dn, attr, value )) == -1 )
473                                 ldap_perror( ld, "ldap_compare" );
474                         else
475                                 printf( "Compare initiated with id %d\n", id );
476                         break;
477
478                 case 'd':       /* turn on debugging */
479 #ifdef LDAP_DEBUG
480                         getline( line, sizeof(line), stdin, "debug level? " );
481                         ldap_debug = atoi( line );
482 #ifdef LBER_DEBUG
483                         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
484                                 lber_debug = ldap_debug;
485                         }
486 #endif
487 #else
488                         printf( "Compile with -DLDAP_DEBUG for debugging\n" );
489 #endif
490                         break;
491
492                 case 'E':       /* explode a dn */
493                         getline( line, sizeof(line), stdin, "dn? " );
494                         exdn = ldap_explode_dn( line, 0 );
495                         for ( i = 0; exdn != NULL && exdn[i] != NULL; i++ ) {
496                                 printf( "\t%s\n", exdn[i] );
497                         }
498                         break;
499
500                 case 'g':       /* set next msgid */
501                         getline( line, sizeof(line), stdin, "msgid? " );
502                         ld->ld_msgid = atoi( line );
503                         break;
504
505                 case 'v':       /* set version number */
506                         getline( line, sizeof(line), stdin, "version? " );
507                         ld->ld_version = atoi( line );
508                         break;
509
510                 case 'm':       /* modify or modifyrdn */
511                         if ( strncmp( line, "modify", 4 ) == 0 ) {
512                                 getline( dn, sizeof(dn), stdin, "dn? " );
513                                 strcat( dn, dnsuffix );
514                                 if ( (mods = get_modlist(
515                                     "mod (0=>add, 1=>delete, 2=>replace -1=>done)? ",
516                                     "attribute type? ", "attribute value? " ))
517                                     == NULL )
518                                         break;
519                                 if ( (id = ldap_modify( ld, dn, mods )) == -1 )
520                                         ldap_perror( ld, "ldap_modify" );
521                                 else
522                                         printf( "Modify initiated with id %d\n",
523                                             id );
524                         } else if ( strncmp( line, "modrdn", 4 ) == 0 ) {
525                                 getline( dn, sizeof(dn), stdin, "dn? " );
526                                 strcat( dn, dnsuffix );
527                                 getline( rdn, sizeof(rdn), stdin, "newrdn? " );
528                                 if ( (id = ldap_modrdn( ld, dn, rdn )) == -1 )
529                                         ldap_perror( ld, "ldap_modrdn" );
530                                 else
531                                         printf( "Modrdn initiated with id %d\n",
532                                             id );
533                         } else {
534                                 printf( "Possibilities: [modi]fy, [modr]dn\n" );
535                         }
536                         break;
537
538                 case 'q':       /* quit */
539 #ifdef LDAP_CONNECTIONLESS
540                         if ( cldapflg )
541                                 cldap_close( ld );
542 #endif /* LDAP_CONNECTIONLESS */
543 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
544                         if ( !cldapflg )
545 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
546                         if ( !cldapflg && bound )
547 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
548                                 ldap_unbind( ld );
549                         exit( 0 );
550                         break;
551
552                 case 'r':       /* result or remove */
553                         switch ( command3 ) {
554                         case 's':       /* result */
555                                 getline( line, sizeof(line), stdin,
556                                     "msgid (-1=>any)? " );
557                                 if ( line[0] == '\0' )
558                                         id = -1;
559                                 else
560                                         id = atoi( line );
561                                 getline( line, sizeof(line), stdin,
562                                     "all (0=>any, 1=>all)? " );
563                                 if ( line[0] == '\0' )
564                                         all = 1;
565                                 else
566                                         all = atoi( line );
567                                 if (( msgtype = ldap_result( ld, id, all,
568                                     &timeout, &res )) < 1 ) {
569                                         ldap_perror( ld, "ldap_result" );
570                                         break;
571                                 }
572                                 printf( "\nresult: msgtype %d msgid %d\n",
573                                     msgtype, res->lm_msgid );
574                                 handle_result( ld, res );
575                                 res = NULLMSG;
576                                 break;
577
578                         case 'm':       /* remove */
579                                 getline( dn, sizeof(dn), stdin, "dn? " );
580                                 strcat( dn, dnsuffix );
581                                 if ( (id = ldap_delete( ld, dn )) == -1 )
582                                         ldap_perror( ld, "ldap_delete" );
583                                 else
584                                         printf( "Remove initiated with id %d\n",
585                                             id );
586                                 break;
587
588                         default:
589                                 printf( "Possibilities: [rem]ove, [res]ult\n" );
590                                 break;
591                         }
592                         break;
593
594                 case 's':       /* search */
595                         getline( dn, sizeof(dn), stdin, "searchbase? " );
596                         strcat( dn, dnsuffix );
597                         getline( line, sizeof(line), stdin,
598                             "scope (0=Base, 1=One Level, 2=Subtree)? " );
599                         scope = atoi( line );
600                         getline( filter, sizeof(filter), stdin,
601                             "search filter (e.g. sn=jones)? " );
602                         types = get_list( "attrs to return? " );
603                         getline( line, sizeof(line), stdin,
604                             "attrsonly (0=attrs&values, 1=attrs only)? " );
605                         attrsonly = atoi( line );
606
607                         if ( cldapflg ) {
608 #ifdef LDAP_CONNECTIONLESS
609                             getline( line, sizeof(line), stdin,
610                                 "Requestor DN (for logging)? " );
611                             if ( cldap_search_s( ld, dn, scope, filter, types,
612                                     attrsonly, &res, line ) != 0 ) {
613                                 ldap_perror( ld, "cldap_search_s" );
614                             } else {
615                                 printf( "\nresult: msgid %d\n",
616                                     res->lm_msgid );
617                                 handle_result( ld, res );
618                                 res = NULLMSG;
619                             }
620 #endif /* LDAP_CONNECTIONLESS */
621                         } else {
622                             if (( id = ldap_search( ld, dn, scope, filter,
623                                     types, attrsonly  )) == -1 ) {
624                                 ldap_perror( ld, "ldap_search" );
625                             } else {
626                                 printf( "Search initiated with id %d\n", id );
627                             }
628                         }
629                         free_list( types );
630                         break;
631
632                 case 't':       /* set timeout value */
633                         getline( line, sizeof(line), stdin, "timeout? " );
634                         timeout.tv_sec = atoi( line );
635                         break;
636
637                 case 'U':       /* set ufn search prefix */
638                         getline( line, sizeof(line), stdin, "ufn prefix? " );
639                         ldap_ufn_setprefix( ld, line );
640                         break;
641
642                 case 'u':       /* user friendly search w/optional timeout */
643                         getline( dn, sizeof(dn), stdin, "ufn? " );
644                         strcat( dn, dnsuffix );
645                         types = get_list( "attrs to return? " );
646                         getline( line, sizeof(line), stdin,
647                             "attrsonly (0=attrs&values, 1=attrs only)? " );
648                         attrsonly = atoi( line );
649
650                         if ( command2 == 't' ) {
651                                 id = ldap_ufn_search_c( ld, dn, types,
652                                     attrsonly, &res, ldap_ufn_timeout,
653                                     &timeout );
654                         } else {
655                                 id = ldap_ufn_search_s( ld, dn, types,
656                                     attrsonly, &res );
657                         }
658                         if ( res == NULL )
659                                 ldap_perror( ld, "ldap_ufn_search" );
660                         else {
661                                 printf( "\nresult: err %d\n", id );
662                                 handle_result( ld, res );
663                                 res = NULLMSG;
664                         }
665                         free_list( types );
666                         break;
667
668                 case 'l':       /* URL search */
669                         getline( line, sizeof(line), stdin,
670                             "attrsonly (0=attrs&values, 1=attrs only)? " );
671                         attrsonly = atoi( line );
672                         getline( line, sizeof(line), stdin, "LDAP URL? " );
673                         if (( id = ldap_url_search( ld, line, attrsonly  ))
674                                 == -1 ) {
675                             ldap_perror( ld, "ldap_url_search" );
676                         } else {
677                             printf( "URL search initiated with id %d\n", id );
678                         }
679                         break;
680
681                 case 'p':       /* parse LDAP URL */
682                         getline( line, sizeof(line), stdin, "LDAP URL? " );
683                         if (( i = ldap_url_parse( line, &ludp )) != 0 ) {
684                             fprintf( stderr, "ldap_url_parse: error %d\n", i );
685                         } else {
686                             printf( "\t  host: " );
687                             if ( ludp->lud_host == NULL ) {
688                                 printf( "DEFAULT\n" );
689                             } else {
690                                 printf( "<%s>\n", ludp->lud_host );
691                             }
692                             printf( "\t  port: " );
693                             if ( ludp->lud_port == 0 ) {
694                                 printf( "DEFAULT\n" );
695                             } else {
696                                 printf( "%d\n", ludp->lud_port );
697                             }
698                             printf( "\t    dn: <%s>\n", ludp->lud_dn );
699                             printf( "\t attrs:" );
700                             if ( ludp->lud_attrs == NULL ) {
701                                 printf( " ALL" );
702                             } else {
703                                 for ( i = 0; ludp->lud_attrs[ i ] != NULL; ++i ) {
704                                     printf( " <%s>", ludp->lud_attrs[ i ] );
705                                 }
706                             }
707                             printf( "\n\t scope: %s\n", ludp->lud_scope == LDAP_SCOPE_ONELEVEL ?
708                                 "ONE" : ludp->lud_scope == LDAP_SCOPE_BASE ? "BASE" :
709                                 ludp->lud_scope == LDAP_SCOPE_SUBTREE ? "SUB" : "**invalid**" );
710                             printf( "\tfilter: <%s>\n", ludp->lud_filter );
711                             ldap_free_urldesc( ludp );
712                         }
713                             break;
714
715                 case 'n':       /* set dn suffix, for convenience */
716                         getline( line, sizeof(line), stdin, "DN suffix? " );
717                         strcpy( dnsuffix, line );
718                         break;
719
720                 case 'e':       /* enable cache */
721 #ifdef LDAP_NOCACHE
722                         printf( NOCACHEERRMSG );
723 #else /* LDAP_NOCACHE */
724                         getline( line, sizeof(line), stdin, "Cache timeout (secs)? " );
725                         i = atoi( line );
726                         getline( line, sizeof(line), stdin, "Maximum memory to use (bytes)? " );
727                         if ( ldap_enable_cache( ld, i, atoi( line )) == 0 ) {
728                                 printf( "local cache is on\n" ); 
729                         } else {
730                                 printf( "ldap_enable_cache failed\n" ); 
731                         }
732 #endif /* LDAP_NOCACHE */
733                         break;
734
735                 case 'x':       /* uncache entry */
736 #ifdef LDAP_NOCACHE
737                         printf( NOCACHEERRMSG );
738 #else /* LDAP_NOCACHE */
739                         getline( line, sizeof(line), stdin, "DN? " );
740                         ldap_uncache_entry( ld, line );
741 #endif /* LDAP_NOCACHE */
742                         break;
743
744                 case 'X':       /* uncache request */
745 #ifdef LDAP_NOCACHE
746                         printf( NOCACHEERRMSG );
747 #else /* LDAP_NOCACHE */
748                         getline( line, sizeof(line), stdin, "request msgid? " );
749                         ldap_uncache_request( ld, atoi( line ));
750 #endif /* LDAP_NOCACHE */
751                         break;
752
753                 case 'o':       /* set ldap options */
754                         getline( line, sizeof(line), stdin, "alias deref (0=never, 1=searching, 2=finding, 3=always)?" );
755                         ld->ld_deref = atoi( line );
756                         getline( line, sizeof(line), stdin, "timelimit?" );
757                         ld->ld_timelimit = atoi( line );
758                         getline( line, sizeof(line), stdin, "sizelimit?" );
759                         ld->ld_sizelimit = atoi( line );
760
761                         LDAP_BOOL_ZERO(&ld->ld_options);
762
763 #ifdef STR_TRANSLATION
764                         getline( line, sizeof(line), stdin,
765                                 "Automatic translation of T.61 strings (0=no, 1=yes)?" );
766                         if ( atoi( line ) == 0 ) {
767                                 ld->ld_lberoptions &= ~LBER_TRANSLATE_STRINGS;
768                         } else {
769                                 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
770 #ifdef LDAP_CHARSET_8859
771                                 getline( line, sizeof(line), stdin,
772                                         "Translate to/from ISO-8859 (0=no, 1=yes?" );
773                                 if ( atoi( line ) != 0 ) {
774                                         ldap_set_string_translators( ld,
775                                             ldap_8859_to_t61,
776                                             ldap_t61_to_8859 );
777                                 }
778 #endif /* LDAP_CHARSET_8859 */
779                         }
780 #endif /* STR_TRANSLATION */
781
782 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
783                         getline( line, sizeof(line), stdin,
784                                 "Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
785                         if ( atoi( line ) != 0 ) {
786                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS);
787                         }
788 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
789
790 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
791                         getline( line, sizeof(line), stdin,
792                                 "Recognize and chase referrals (0=no, 1=yes)?" );
793                         if ( atoi( line ) != 0 ) {
794                                 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
795                                 getline( line, sizeof(line), stdin,
796                                         "Prompt for bind credentials when chasing referrals (0=no, 1=yes)?" );
797                                 if ( atoi( line ) != 0 ) {
798                                         ldap_set_rebind_proc( ld, bind_prompt );
799                                 }
800                         }
801 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
802                         break;
803
804                 case 'O':       /* set cache options */
805 #ifdef LDAP_NOCACHE
806                         printf( NOCACHEERRMSG );
807 #else /* LDAP_NOCACHE */
808                         getline( line, sizeof(line), stdin, "cache errors (0=smart, 1=never, 2=always)?" );
809                         switch( atoi( line )) {
810                         case 0:
811                                 ldap_set_cache_options( ld, 0 );
812                                 break;
813                         case 1:
814                                 ldap_set_cache_options( ld,
815                                         LDAP_CACHE_OPT_CACHENOERRS );
816                                 break;
817                         case 2:
818                                 ldap_set_cache_options( ld,
819                                         LDAP_CACHE_OPT_CACHEALLERRS );
820                                 break;
821                         default:
822                                 printf( "not a valid cache option\n" );
823                         }
824 #endif /* LDAP_NOCACHE */
825                         break;
826
827                 case '?':       /* help */
828     printf( "Commands: [ad]d         [ab]andon         [b]ind\n" );
829     printf( "          [B]ind async  [c]ompare         [l]URL search\n" );
830     printf( "          [modi]fy      [modr]dn          [rem]ove\n" );
831     printf( "          [res]ult      [s]earch          [q]uit/unbind\n\n" );
832     printf( "          [u]fn search  [ut]fn search with timeout\n" );
833     printf( "          [d]ebug       [e]nable cache    set ms[g]id\n" );
834     printf( "          d[n]suffix    [t]imeout         [v]ersion\n" );
835     printf( "          [U]fn prefix  [x]uncache entry  [X]uncache request\n" );
836     printf( "          [?]help       [o]ptions         [O]cache options\n" );
837     printf( "          [E]xplode dn  [p]arse LDAP URL\n" );
838                         break;
839
840                 default:
841                         printf( "Invalid command.  Type ? for help.\n" );
842                         break;
843                 }
844
845                 (void) memset( line, '\0', sizeof(line) );
846         }
847
848         return( 0 );
849 }
850
851 static void
852 handle_result( LDAP *ld, LDAPMessage *lm )
853 {
854         switch ( lm->lm_msgtype ) {
855         case LDAP_RES_COMPARE:
856                 printf( "Compare result\n" );
857                 print_ldap_result( ld, lm, "compare" );
858                 break;
859
860         case LDAP_RES_SEARCH_RESULT:
861                 printf( "Search result\n" );
862                 print_ldap_result( ld, lm, "search" );
863                 break;
864
865         case LDAP_RES_SEARCH_ENTRY:
866                 printf( "Search entry\n" );
867                 print_search_entry( ld, lm );
868                 break;
869
870         case LDAP_RES_ADD:
871                 printf( "Add result\n" );
872                 print_ldap_result( ld, lm, "add" );
873                 break;
874
875         case LDAP_RES_DELETE:
876                 printf( "Delete result\n" );
877                 print_ldap_result( ld, lm, "delete" );
878                 break;
879
880         case LDAP_RES_MODRDN:
881                 printf( "ModRDN result\n" );
882                 print_ldap_result( ld, lm, "modrdn" );
883                 break;
884
885         case LDAP_RES_BIND:
886                 printf( "Bind result\n" );
887                 print_ldap_result( ld, lm, "bind" );
888                 break;
889
890         default:
891                 printf( "Unknown result type 0x%x\n", lm->lm_msgtype );
892                 print_ldap_result( ld, lm, "unknown" );
893         }
894 }
895
896 static void
897 print_ldap_result( LDAP *ld, LDAPMessage *lm, char *s )
898 {
899         ldap_result2error( ld, lm, 1 );
900         ldap_perror( ld, s );
901 /*
902         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
903                 fprintf( stderr, "Additional info: %s\n", ld->ld_error );
904         if ( NAME_ERROR( ld->ld_errno ) && ld->ld_matched != NULL )
905                 fprintf( stderr, "Matched DN: %s\n", ld->ld_matched );
906 */
907 }
908
909 static void
910 print_search_entry( LDAP *ld, LDAPMessage *res )
911 {
912         BerElement      *ber;
913         char            *a, *dn, *ufn;
914         struct berval   **vals;
915         int             i;
916         LDAPMessage     *e;
917
918         for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
919             e = ldap_next_entry( ld, e ) ) {
920                 if ( e->lm_msgtype == LDAP_RES_SEARCH_RESULT )
921                         break;
922
923                 dn = ldap_get_dn( ld, e );
924                 printf( "\tDN: %s\n", dn );
925
926                 ufn = ldap_dn2ufn( dn );
927                 printf( "\tUFN: %s\n", ufn );
928
929                 free( dn );
930                 free( ufn );
931
932                 for ( a = ldap_first_attribute( ld, e, &ber ); a != NULL;
933                     a = ldap_next_attribute( ld, e, ber ) ) {
934                         printf( "\t\tATTR: %s\n", a );
935                         if ( (vals = ldap_get_values_len( ld, e, a ))
936                             == NULL ) {
937                                 printf( "\t\t\t(no values)\n" );
938                         } else {
939                                 for ( i = 0; vals[i] != NULL; i++ ) {
940                                         int     j, nonascii;
941
942                                         nonascii = 0;
943                                         for ( j = 0; (unsigned long) j < vals[i]->bv_len; j++ )
944                                                 if ( !isascii( vals[i]->bv_val[j] ) ) {
945                                                         nonascii = 1;
946                                                         break;
947                                                 }
948
949                                         if ( nonascii ) {
950                                                 printf( "\t\t\tlength (%ld) (not ascii)\n", vals[i]->bv_len );
951 #ifdef BPRINT_NONASCII
952                                                 lber_bprint( vals[i]->bv_val,
953                                                     vals[i]->bv_len );
954 #endif /* BPRINT_NONASCII */
955                                                 continue;
956                                         }
957                                         printf( "\t\t\tlength (%ld) %s\n",
958                                             vals[i]->bv_len, vals[i]->bv_val );
959                                 }
960                                 ber_bvecfree( vals );
961                         }
962                 }
963         }
964
965         if ( res->lm_msgtype == LDAP_RES_SEARCH_RESULT
966             || res->lm_chain != NULLMSG )
967                 print_ldap_result( ld, res, "search" );
968 }