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