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