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