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