3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
34 #include "ldap_defaults.h"
37 usage( const char *s )
40 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
41 "\tfilter\tRFC-2254 compliant LDAP search filter\n"
42 "\tattributes\twhitespace-separated list of attribute descriptions\n"
43 "\t which may include:\n"
44 "\t\t1.1 -- no attributes\n"
45 "\t\t* -- all user attributes\n"
46 "\t\t+ -- all operational attributes\n"
48 "\t-a deref\tdereference aliases: never (default), always, search, or find\n"
49 "\t-A\t\tretrieve attribute names only (no values)\n"
50 "\t-b basedn\tbase dn for search\n"
51 "\t-d level\tset LDAP debugging level to `level'\n"
52 "\t-D binddn\tbind DN\n"
53 "\t-E\t\trequest SASL privacy (-EE to make it critical)\n"
54 "\t-f file\t\tperform sequence of searches listed in `file'\n"
55 "\t-h host\t\tLDAP server\n"
56 "\t-I\t\trequest SASL integrity checking (-II to make it\n"
58 "\t-k\t\tuse Kerberos authentication\n"
59 "\t-K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
60 "\t-l limit\ttime limit (in seconds) for search\n"
61 "\t-L\t\tprint responses in LDIFv1 format\n"
62 "\t-LL\t\tprint responses in LDIF format without comments\n"
63 "\t-LLL\t\tprint responses in LDIF format without comments\n"
65 "\t-M\t\tenable Manage DSA IT control (-MM to make critical)\n"
66 "\t-n\t\tshow what would be done but don't actually search\n"
67 "\t-p port\t\tport on LDAP server\n"
68 "\t-P version\tprocotol version (default: 3)\n"
69 "\t-R\t\tdo not automatically follow referrals\n"
70 "\t-s scope\tone of base, one, or sub (search scope)\n"
71 "\t-S attr\t\tsort the results by attribute `attr'\n"
72 "\t-t\t\twrite binary values to files in temporary directory\n"
73 "\t-tt\t\twrite all values to files in temporary directory\n"
74 "\t-T path\t\twrite files to directory specified by path (default:\n"
75 "\t\t\t\"" LDAP_TMPDIR "\")\n"
76 "\t-u\t\tinclude User Friendly entry names in the output\n"
77 "\t-U user\t\tSASL authentication identity (username)\n"
78 "\t-v\t\trun in verbose mode (diagnostics to standard output)\n"
79 "\t-V prefix\tURL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
80 "\t-w passwd\tbind passwd (for simple authentication)\n"
81 "\t-W\t\tprompt for bind passwd\n"
82 "\t-X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
83 "\t-Y mech\t\tSASL mechanism\n"
84 "\t-z limit\tsize limit (in entries) for search\n"
85 "\t-Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
91 static void print_entry LDAP_P((
96 static void print_reference(
98 LDAPMessage *reference );
100 static void print_extended(
102 LDAPMessage *extended );
104 static void print_partial(
106 LDAPMessage *partial );
108 static int print_result(
113 static void print_ctrls(
114 LDAPControl **ctrls );
116 static int write_ldif LDAP_P((
122 static int dosearch LDAP_P((
131 static char *tmpdir = NULL;
132 static char *urlpre = NULL;
134 static char *binddn = NULL;
135 static struct berval passwd = { 0, NULL };
136 static char *base = NULL;
137 static char *ldaphost = NULL;
138 static int ldapport = 0;
139 #ifdef HAVE_CYRUS_SASL
140 static char *sasl_authc_id = NULL;
141 static char *sasl_authz_id = NULL;
142 static char *sasl_mech = NULL;
143 static int sasl_integrity = 0;
144 static int sasl_privacy = 0;
146 static int use_tls = 0;
147 static char *sortattr = NULL;
148 static int verbose, not, includeufn, vals2tmp, ldif;
151 main( int argc, char **argv )
153 char *infile, *filtpattern, **attrs, line[BUFSIZ];
155 int rc, i, first, scope, deref, attrsonly, manageDSAit;
156 int referrals, timelimit, sizelimit, debug;
157 int authmethod, version, want_bindpw;
161 debug = verbose = not = vals2tmp =
162 attrsonly = manageDSAit = ldif = want_bindpw = 0;
164 deref = sizelimit = timelimit = version = -1;
166 /* default should be off */
169 scope = LDAP_SCOPE_SUBTREE;
170 authmethod = LDAP_AUTH_SIMPLE;
172 while (( i = getopt( argc, argv,
173 "Aa:b:D:d:Ef:h:IKkLl:MnP:p:RS:s:T:tU:uV:vWw:X:Y:Zz:")) != EOF )
176 case 'n': /* do nothing */
179 case 'v': /* verbose mode */
183 debug |= atoi( optarg );
185 case 'k': /* use kerberos bind */
186 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
187 authmethod = LDAP_AUTH_KRBV4;
189 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
190 return( EXIT_FAILURE );
193 case 'K': /* use kerberos bind, 1st part only */
194 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
195 authmethod = LDAP_AUTH_KRBV41;
197 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
198 return( EXIT_FAILURE );
202 case 'u': /* include UFN */
205 case 't': /* write attribute values to TMPDIR files */
209 /* enable Manage DSA IT */
212 case 'R': /* don't automatically chase referrals */
215 case 'A': /* retrieve attribute names only -- no values */
218 case 'L': /* print entries in LDIF format */
222 case 's': /* search scope */
223 if ( strcasecmp( optarg, "base" ) == 0 ) {
224 scope = LDAP_SCOPE_BASE;
225 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
226 scope = LDAP_SCOPE_ONELEVEL;
227 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
228 scope = LDAP_SCOPE_SUBTREE;
230 fprintf( stderr, "scope should be base, one, or sub\n" );
235 case 'a': /* set alias deref option */
236 if ( strcasecmp( optarg, "never" ) == 0 ) {
237 deref = LDAP_DEREF_NEVER;
238 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
239 deref = LDAP_DEREF_SEARCHING;
240 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
241 deref = LDAP_DEREF_FINDING;
242 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
243 deref = LDAP_DEREF_ALWAYS;
245 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
250 case 'T': /* tmpdir */
251 if( tmpdir ) free( tmpdir );
252 tmpdir = strdup( optarg );
254 case 'V': /* uri prefix */
255 if( urlpre ) free( urlpre );
256 urlpre = strdup( optarg );
258 case 'f': /* input file */
259 infile = strdup( optarg );
261 case 'h': /* ldap host */
262 ldaphost = strdup( optarg );
264 case 'b': /* search base */
265 base = strdup( optarg );
267 case 'D': /* bind DN */
268 binddn = strdup( optarg );
270 case 'p': /* ldap port */
271 ldapport = atoi( optarg );
273 case 'w': /* bind password */
274 passwd.bv_val = strdup( optarg );
278 for( p = optarg; *p == '\0'; p++ ) {
282 passwd.bv_len = strlen( passwd.bv_val );
284 case 'l': /* time limit */
285 timelimit = atoi( optarg );
287 case 'z': /* size limit */
288 sizelimit = atoi( optarg );
290 case 'S': /* sort attribute */
291 sortattr = strdup( optarg );
297 switch( atoi( optarg ) )
300 version = LDAP_VERSION2;
303 version = LDAP_VERSION3;
306 fprintf( stderr, "protocol version should be 2 or 3\n" );
311 #ifdef HAVE_CYRUS_SASL
313 authmethod = LDAP_AUTH_SASL;
315 fprintf( stderr, "%s was not compiled with SASL support\n",
317 return( EXIT_FAILURE );
321 #ifdef HAVE_CYRUS_SASL
323 authmethod = LDAP_AUTH_SASL;
325 fprintf( stderr, "%s was not compiled with SASL support\n",
327 return( EXIT_FAILURE );
331 #ifdef HAVE_CYRUS_SASL
332 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
333 sasl_mech = strdup( optarg );
335 authmethod = LDAP_AUTH_SASL;
337 fprintf( stderr, "%s was not compiled with SASL support\n",
339 return( EXIT_FAILURE );
343 #ifdef HAVE_CYRUS_SASL
344 sasl_authc_id = strdup( optarg );
345 authmethod = LDAP_AUTH_SASL;
347 fprintf( stderr, "%s was not compiled with SASL support\n",
349 return( EXIT_FAILURE );
353 #ifdef HAVE_CYRUS_SASL
354 sasl_authz_id = strdup( optarg );
355 authmethod = LDAP_AUTH_SASL;
357 fprintf( stderr, "%s was not compiled with SASL support\n",
359 return( EXIT_FAILURE );
366 fprintf( stderr, "%s was not compiled with TLS support\n",
368 return( EXIT_FAILURE );
376 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
377 LDAP_AUTH_KRBV41 ) ) {
378 if( version > LDAP_VERSION2 ) {
379 fprintf( stderr, "Kerberos requires LDAPv2\n" );
380 return( EXIT_FAILURE );
382 version = LDAP_VERSION2;
384 else if ( authmethod == LDAP_AUTH_SASL ) {
385 if( version != -1 && version != LDAP_VERSION3 ) {
386 fprintf( stderr, "SASL requires LDAPv3\n" );
387 return( EXIT_FAILURE );
389 version = LDAP_VERSION3;
393 if( version != -1 && version != LDAP_VERSION3 ) {
394 fprintf(stderr, "manage DSA control requires LDAPv3\n");
397 version = LDAP_VERSION3;
401 if( version != -1 && version != LDAP_VERSION3 ) {
402 fprintf(stderr, "Start TLS requires LDAPv3\n");
405 version = LDAP_VERSION3;
408 if ( argc - optind < 1 ) {
409 filtpattern = "(objectclass=*)";
411 filtpattern = strdup( argv[optind++] );
414 if ( argv[optind] == NULL ) {
416 } else if ( sortattr == NULL || *sortattr == '\0' ) {
417 attrs = &argv[optind];
420 if ( infile != NULL ) {
421 if ( infile[0] == '-' && infile[1] == '\0' ) {
423 } else if (( fp = fopen( infile, "r" )) == NULL ) {
430 && (tmpdir = getenv("TMPDIR")) == NULL
431 && (tmpdir = getenv("TMP")) == NULL
432 && (tmpdir = getenv("TEMP")) == NULL )
434 tmpdir = LDAP_TMPDIR;
437 if( urlpre == NULL ) {
438 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
440 if( urlpre == NULL ) {
445 sprintf( urlpre, "file:///%s/",
446 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
448 /* urlpre should be URLized.... */
452 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
453 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
455 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
456 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
462 (void) SIGNAL( SIGPIPE, SIG_IGN );
467 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
468 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
472 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
473 perror( "ldap_init" );
474 return( EXIT_FAILURE );
478 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
480 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
482 if (timelimit != -1 &&
483 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
485 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
487 if (sizelimit != -1 &&
488 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
490 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
492 if (referrals != -1 &&
493 ldap_set_option( ld, LDAP_OPT_REFERRALS,
494 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
496 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
497 referrals ? "on" : "off" );
500 if (version == -1 ) {
504 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
505 != LDAP_OPT_SUCCESS )
507 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
511 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
513 ldap_perror( ld, "ldap_start_tls" );
514 return( EXIT_FAILURE );
519 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
520 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
523 if ( authmethod == LDAP_AUTH_SASL ) {
524 #ifdef HAVE_CYRUS_SASL
525 int minssf = 0, maxssf = 0;
527 if ( sasl_integrity > 0 )
529 if ( sasl_integrity > 1 )
531 if ( sasl_privacy > 0 )
532 maxssf = 100000; /* Something big value */
533 if ( sasl_privacy > 1 )
536 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
537 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
538 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
540 return( EXIT_FAILURE );
542 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
543 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
544 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
546 return( EXIT_FAILURE );
549 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
550 sasl_authz_id, sasl_mech,
551 passwd.bv_len ? &passwd : NULL,
554 if( rc != LDAP_SUCCESS ) {
555 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
556 return( EXIT_FAILURE );
559 fprintf( stderr, "%s was not compiled with SASL support\n",
561 return( EXIT_FAILURE );
564 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
566 ldap_perror( ld, "ldap_bind" );
567 return( EXIT_FAILURE );
574 LDAPControl *ctrls[2];
578 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
579 c.ldctl_value.bv_val = NULL;
580 c.ldctl_value.bv_len = 0;
581 c.ldctl_iscritical = manageDSAit > 1;
583 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
585 if( err != LDAP_OPT_SUCCESS ) {
586 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
587 if( c.ldctl_iscritical ) {
588 exit( EXIT_FAILURE );
594 fprintf( stderr, "filter%s: %s\nrequesting: ",
595 infile != NULL ? " pattern" : "",
598 if ( attrs == NULL ) {
599 fprintf( stderr, "ALL" );
601 for ( i = 0; attrs[ i ] != NULL; ++i ) {
602 fprintf( stderr, "%s ", attrs[ i ] );
605 fprintf( stderr, "\n" );
609 printf( "version: %d\n\n", ldif ? 1 : 2 );
613 printf( "#\n# filter%s: %s\n# requesting: ",
614 infile != NULL ? " pattern" : "",
617 if ( attrs == NULL ) {
620 for ( i = 0; attrs[ i ] != NULL; ++i ) {
621 printf( "%s ", attrs[ i ] );
626 printf("\n# with manageDSAit %scontrol",
627 manageDSAit > 1 ? "critical " : "" );
633 if ( infile == NULL ) {
634 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
639 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
640 line[ strlen( line ) - 1 ] = '\0';
646 rc = dosearch( ld, base, scope, attrs, attrsonly,
668 char filter[ BUFSIZ ];
675 LDAPMessage *res, *msg;
678 if( filtpatt != NULL ) {
679 sprintf( filter, filtpatt, value );
682 fprintf( stderr, "filter is: (%s)\n", filter );
686 printf( "#\n# filter: %s\n#\n", filter );
690 sprintf( filter, "%s", value );
697 msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly );
700 ldap_perror( ld, "ldap_search" );
701 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
705 nresponses = nentries = nreferences = nextended = npartial = 0;
708 while ((rc = ldap_result( ld, LDAP_RES_ANY,
709 sortattr ? 1 : 0, NULL, &res )) > 0 )
712 (void) ldap_sort_entries( ld, &res,
713 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
716 for ( msg = ldap_first_message( ld, res );
718 msg = ldap_next_message( ld, msg ) )
720 if( nresponses++ ) putchar('\n');
722 switch( ldap_msgtype( msg ) ) {
723 case LDAP_RES_SEARCH_ENTRY:
725 print_entry( ld, msg, attrsonly );
728 case LDAP_RES_SEARCH_REFERENCE:
730 print_reference( ld, msg );
733 case LDAP_RES_EXTENDED:
735 print_extended( ld, msg );
737 if( ldap_msgid( msg ) == 0 ) {
738 /* unsolicited extended operation */
743 case LDAP_RES_EXTENDED_PARTIAL:
745 print_partial( ld, msg );
748 case LDAP_RES_SEARCH_RESULT:
749 rc = print_result( ld, msg, 1 );
758 ldap_perror( ld, "ldap_result" );
764 printf( "\n# numResponses: %d\n", nresponses );
765 if( nentries ) printf( "# numEntries: %d\n", nentries );
766 if( nextended ) printf( "# numExtended: %d\n", nextended );
767 if( npartial ) printf( "# numPartial: %d\n", npartial );
768 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
781 char tmpfname[ 256 ];
784 BerElement *ber = NULL;
785 struct berval **bvals;
786 LDAPControl **ctrls = NULL;
789 dn = ldap_get_dn( ld, entry );
793 ufn = ldap_dn2ufn( dn );
794 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
796 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
798 rc = ldap_get_entry_controls( ld, entry, &ctrls );
800 if( rc != LDAP_SUCCESS ) {
801 fprintf(stderr, "print_entry: %d\n", rc );
802 ldap_perror( ld, "ldap_get_entry_controls" );
803 exit( EXIT_FAILURE );
807 print_ctrls( ctrls );
808 ldap_controls_free( ctrls );
813 ufn = ldap_dn2ufn( dn );
815 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
818 if( ufn != NULL ) ldap_memfree( ufn );
821 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
822 a = ldap_next_attribute( ld, entry, ber ) )
825 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
827 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
828 for ( i = 0; bvals[i] != NULL; i++ ) {
829 if ( vals2tmp > 1 || ( vals2tmp
830 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
833 /* write value to file */
834 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
838 if ( mktemp( tmpfname ) == NULL ) {
843 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
848 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
853 if ( fwrite( bvals[ i ]->bv_val,
854 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
863 sprintf( url, "%s%s", urlpre,
864 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
866 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
869 write_ldif( LDIF_PUT_VALUE, a,
870 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
873 ber_bvecfree( bvals );
882 static void print_reference(
884 LDAPMessage *reference )
891 printf("# search reference\n");
894 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
896 if( rc != LDAP_SUCCESS ) {
897 ldap_perror(ld, "ldap_parse_reference");
898 exit( EXIT_FAILURE );
903 for( i=0; refs[i] != NULL; i++ ) {
904 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
905 "ref", refs[i], strlen(refs[i]) );
907 ber_memvfree( (void **) refs );
911 print_ctrls( ctrls );
912 ldap_controls_free( ctrls );
916 static void print_extended(
918 LDAPMessage *extended )
922 struct berval *retdata = NULL;
925 printf("# extended result response\n");
928 rc = ldap_parse_extended_result( ld, extended,
929 &retoid, &retdata, 0 );
931 if( rc != LDAP_SUCCESS ) {
932 ldap_perror(ld, "ldap_parse_extended_result");
933 exit( EXIT_FAILURE );
936 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
937 "extended", retoid, retoid ? strlen(retoid) : 0 );
938 ber_memfree( retoid );
941 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
942 "data", retdata->bv_val, retdata->bv_len );
943 ber_bvfree( retdata );
946 print_result( ld, extended, 0 );
949 static void print_partial(
951 LDAPMessage *partial )
955 struct berval *retdata = NULL;
956 LDAPControl **ctrls = NULL;
959 printf("# extended partial response\n");
962 rc = ldap_parse_extended_partial( ld, partial,
963 &retoid, &retdata, &ctrls, 0 );
965 if( rc != LDAP_SUCCESS ) {
966 ldap_perror(ld, "ldap_parse_extended_partial");
967 exit( EXIT_FAILURE );
970 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
971 "partial", retoid, retoid ? strlen(retoid) : 0 );
973 ber_memfree( retoid );
976 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
978 retdata->bv_val, retdata->bv_len );
980 ber_bvfree( retdata );
984 print_ctrls( ctrls );
985 ldap_controls_free( ctrls );
989 static int print_result(
991 LDAPMessage *result, int search )
996 char *matcheddn = NULL;
999 LDAPControl **ctrls = NULL;
1003 printf("# search result\n");
1006 printf("%s: %d\n", "search", ldap_msgid(result) );
1010 rc = ldap_parse_result( ld, result,
1011 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1013 if( rc != LDAP_SUCCESS ) {
1014 ldap_perror(ld, "ldap_parse_result");
1015 exit( EXIT_FAILURE );
1020 printf( "result: %d %s\n", err, ldap_err2string(err) );
1022 } else if ( err != LDAP_SUCCESS ) {
1023 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1026 if( matcheddn && *matcheddn ) {
1028 write_ldif( LDIF_PUT_VALUE,
1029 "matchedDN", matcheddn, strlen(matcheddn) );
1031 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1034 ber_memfree( matcheddn );
1037 if( text && *text ) {
1039 write_ldif( LDIF_PUT_TEXT, "text",
1040 text, strlen(text) );
1042 fprintf( stderr, "Additional information: %s\n", text );
1045 ber_memfree( text );
1050 for( i=0; refs[i] != NULL; i++ ) {
1052 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1054 fprintf( stderr, "Referral: %s", refs[i] );
1058 ber_memvfree( (void **) refs );
1062 print_ctrls( ctrls );
1063 ldap_controls_free( ctrls );
1069 void print_ctrls( LDAPControl **ctrls ) {
1071 for(i=0; ctrls[i] != NULL; i++ ) {
1072 /* control: OID criticality base64value */
1073 struct berval *b64 = NULL;
1077 len = strlen( ctrls[i]->ldctl_oid );
1079 /* add enough for space after OID and the critical value itself */
1080 len += ctrls[i]->ldctl_iscritical
1081 ? sizeof("true") : sizeof("false");
1083 /* convert to base64 */
1084 if( ctrls[i]->ldctl_value.bv_len ) {
1085 b64 = ber_memalloc( sizeof(struct berval) );
1087 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1088 ctrls[i]->ldctl_value.bv_len ) + 1;
1089 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1091 b64->bv_len = lutil_b64_ntop(
1092 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1093 b64->bv_val, b64->bv_len );
1097 len += 1 + b64->bv_len;
1100 str = malloc( len + 1 );
1101 strcpy( str, ctrls[i]->ldctl_oid );
1102 strcat( str, ctrls[i]->ldctl_iscritical
1103 ? " true" : " false" );
1107 strcat(str, b64->bv_val );
1110 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1111 "control", str, len );
1119 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1123 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1127 fputs( ldif, stdout );
1128 ber_memfree( ldif );