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