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