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 "lutil_ldap.h"
35 #include "ldap_defaults.h"
38 usage( const char *s )
41 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
42 "\tfilter\tRFC-2254 compliant LDAP search filter\n"
43 "\tattributes\twhitespace-separated list of attribute descriptions\n"
44 "\t which may include:\n"
45 "\t\t1.1 -- no attributes\n"
46 "\t\t* -- all user attributes\n"
47 "\t\t+ -- all operational attributes\n"
49 "\t-a deref\tdereference aliases: never (default), always, search, or find\n"
50 "\t-A\t\tretrieve attribute names only (no values)\n"
51 "\t-b basedn\tbase dn for search\n"
52 "\t-d level\tset LDAP debugging level to `level'\n"
53 "\t-D binddn\tbind DN\n"
54 "\t-E\t\trequest SASL privacy (-EE to make it critical)\n"
55 "\t-f file\t\tperform sequence of searches listed in `file'\n"
56 "\t-h host\t\tLDAP server\n"
57 "\t-I\t\trequest SASL integrity checking (-II to make it\n"
59 "\t-k\t\tuse Kerberos authentication\n"
60 "\t-K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
61 "\t-l limit\ttime limit (in seconds) for search\n"
62 "\t-L\t\tprint responses in LDIFv1 format\n"
63 "\t-LL\t\tprint responses in LDIF format without comments\n"
64 "\t-LLL\t\tprint responses in LDIF format without comments\n"
66 "\t-M\t\tenable Manage DSA IT control (-MM to make critical)\n"
67 "\t-n\t\tshow what would be done but don't actually search\n"
68 "\t-O secprops\tSASL security properties\n"
69 "\t-p port\t\tport on LDAP server\n"
70 "\t-P version\tprocotol version (default: 3)\n"
71 "\t-s scope\tone of base, one, or sub (search scope)\n"
72 "\t-S attr\t\tsort the results by attribute `attr'\n"
73 "\t-t\t\twrite binary values to files in temporary directory\n"
74 "\t-tt\t\twrite all values to files in temporary directory\n"
75 "\t-T path\t\twrite files to directory specified by path (default:\n"
76 "\t\t\t\"" LDAP_TMPDIR "\")\n"
77 "\t-u\t\tinclude User Friendly entry names in the output\n"
78 "\t-U user\t\tSASL authentication identity (username)\n"
79 "\t-v\t\trun in verbose mode (diagnostics to standard output)\n"
80 "\t-V prefix\tURL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
81 "\t-w passwd\tbind passwd (for simple authentication)\n"
82 "\t-W\t\tprompt for bind passwd\n"
83 "\t-X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
84 "\t-Y mech\t\tSASL mechanism\n"
85 "\t-z limit\tsize limit (in entries) for search\n"
86 "\t-Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
92 static void print_entry LDAP_P((
97 static void print_reference(
99 LDAPMessage *reference );
101 static void print_extended(
103 LDAPMessage *extended );
105 static void print_partial(
107 LDAPMessage *partial );
109 static int print_result(
114 static void print_ctrls(
115 LDAPControl **ctrls );
117 static int write_ldif LDAP_P((
123 static int dosearch LDAP_P((
131 LDAPControl **sctrls,
132 LDAPControl **cctrls,
133 struct timeval *timelimit,
136 static char *tmpdir = NULL;
137 static char *urlpre = NULL;
139 static char *binddn = NULL;
140 static struct berval passwd = { 0, NULL };
141 static char *base = NULL;
142 static char *ldaphost = NULL;
143 static int ldapport = 0;
144 #ifdef HAVE_CYRUS_SASL
145 static char *sasl_authc_id = NULL;
146 static char *sasl_authz_id = NULL;
147 static char *sasl_mech = NULL;
148 static char *sasl_secprops = NULL;
150 static int use_tls = 0;
151 static char *sortattr = NULL;
152 static int verbose, not, includeufn, vals2tmp, ldif;
155 main( int argc, char **argv )
157 char *infile, *filtpattern, **attrs, line[BUFSIZ];
159 int rc, i, first, scope, deref, attrsonly, manageDSAit;
160 int referrals, timelimit, sizelimit, debug;
161 int authmethod, version, want_bindpw;
165 debug = verbose = not = vals2tmp = referrals =
166 attrsonly = manageDSAit = ldif = want_bindpw = 0;
168 deref = sizelimit = timelimit = version = -1;
170 scope = LDAP_SCOPE_SUBTREE;
171 authmethod = LDAP_AUTH_SIMPLE;
173 while (( i = getopt( argc, argv,
174 "Aa:b:CD:d:f:h:KkLl:MnO:P:p:RS:s:T:tU:uV:vWw:X:Y:Zz:")) != EOF )
177 case 'n': /* do nothing */
180 case 'v': /* verbose mode */
184 debug |= atoi( optarg );
186 case 'k': /* use kerberos bind */
187 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
188 authmethod = LDAP_AUTH_KRBV4;
190 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
191 return( EXIT_FAILURE );
194 case 'K': /* use kerberos bind, 1st part only */
195 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
196 authmethod = LDAP_AUTH_KRBV41;
198 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
199 return( EXIT_FAILURE );
203 case 'u': /* include UFN */
206 case 't': /* write attribute values to TMPDIR files */
210 /* enable Manage DSA IT */
216 case 'R': /* ignore */
218 case 'A': /* retrieve attribute names only -- no values */
221 case 'L': /* print entries in LDIF format */
225 case 's': /* search scope */
226 if ( strcasecmp( optarg, "base" ) == 0 ) {
227 scope = LDAP_SCOPE_BASE;
228 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
229 scope = LDAP_SCOPE_ONELEVEL;
230 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
231 scope = LDAP_SCOPE_SUBTREE;
233 fprintf( stderr, "scope should be base, one, or sub\n" );
238 case 'a': /* set alias deref option */
239 if ( strcasecmp( optarg, "never" ) == 0 ) {
240 deref = LDAP_DEREF_NEVER;
241 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
242 deref = LDAP_DEREF_SEARCHING;
243 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
244 deref = LDAP_DEREF_FINDING;
245 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
246 deref = LDAP_DEREF_ALWAYS;
248 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
253 case 'T': /* tmpdir */
254 if( tmpdir ) free( tmpdir );
255 tmpdir = strdup( optarg );
257 case 'V': /* uri prefix */
258 if( urlpre ) free( urlpre );
259 urlpre = strdup( optarg );
261 case 'f': /* input file */
262 infile = strdup( optarg );
264 case 'h': /* ldap host */
265 ldaphost = strdup( optarg );
267 case 'b': /* search base */
268 base = strdup( optarg );
270 case 'D': /* bind DN */
271 binddn = strdup( optarg );
273 case 'p': /* ldap port */
274 ldapport = atoi( optarg );
276 case 'w': /* bind password */
277 passwd.bv_val = strdup( optarg );
281 for( p = optarg; *p == '\0'; p++ ) {
285 passwd.bv_len = strlen( passwd.bv_val );
287 case 'l': /* time limit */
288 timelimit = atoi( optarg );
290 case 'z': /* size limit */
291 sizelimit = atoi( optarg );
293 case 'S': /* sort attribute */
294 sortattr = strdup( optarg );
300 switch( atoi( optarg ) )
303 version = LDAP_VERSION2;
306 version = LDAP_VERSION3;
309 fprintf( stderr, "protocol version should be 2 or 3\n" );
314 #ifdef HAVE_CYRUS_SASL
315 sasl_secprops = strdup( optarg );
316 authmethod = LDAP_AUTH_SASL;
318 fprintf( stderr, "%s was not compiled with SASL support\n",
320 return( EXIT_FAILURE );
324 #ifdef HAVE_CYRUS_SASL
325 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
326 sasl_mech = strdup( optarg );
328 authmethod = LDAP_AUTH_SASL;
330 fprintf( stderr, "%s was not compiled with SASL support\n",
332 return( EXIT_FAILURE );
336 #ifdef HAVE_CYRUS_SASL
337 sasl_authc_id = strdup( optarg );
338 authmethod = LDAP_AUTH_SASL;
340 fprintf( stderr, "%s was not compiled with SASL support\n",
342 return( EXIT_FAILURE );
346 #ifdef HAVE_CYRUS_SASL
347 sasl_authz_id = strdup( optarg );
348 authmethod = LDAP_AUTH_SASL;
350 fprintf( stderr, "%s was not compiled with SASL support\n",
352 return( EXIT_FAILURE );
359 fprintf( stderr, "%s was not compiled with TLS support\n",
361 return( EXIT_FAILURE );
369 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
370 LDAP_AUTH_KRBV41 ) ) {
371 if( version > LDAP_VERSION2 ) {
372 fprintf( stderr, "Kerberos requires LDAPv2\n" );
373 return( EXIT_FAILURE );
375 version = LDAP_VERSION2;
377 else if ( authmethod == LDAP_AUTH_SASL ) {
378 if( version != -1 && version != LDAP_VERSION3 ) {
379 fprintf( stderr, "SASL requires LDAPv3\n" );
380 return( EXIT_FAILURE );
382 version = LDAP_VERSION3;
386 if( version != -1 && version != LDAP_VERSION3 ) {
387 fprintf(stderr, "manage DSA control requires LDAPv3\n");
390 version = LDAP_VERSION3;
394 if( version != -1 && version != LDAP_VERSION3 ) {
395 fprintf(stderr, "Start TLS requires LDAPv3\n");
398 version = LDAP_VERSION3;
401 if ( argc - optind < 1 ) {
402 filtpattern = "(objectclass=*)";
404 filtpattern = strdup( argv[optind++] );
407 if ( argv[optind] == NULL ) {
409 } else if ( sortattr == NULL || *sortattr == '\0' ) {
410 attrs = &argv[optind];
413 if ( infile != NULL ) {
414 if ( infile[0] == '-' && infile[1] == '\0' ) {
416 } else if (( fp = fopen( infile, "r" )) == NULL ) {
423 && (tmpdir = getenv("TMPDIR")) == NULL
424 && (tmpdir = getenv("TMP")) == NULL
425 && (tmpdir = getenv("TEMP")) == NULL )
427 tmpdir = LDAP_TMPDIR;
430 if( urlpre == NULL ) {
431 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
433 if( urlpre == NULL ) {
438 sprintf( urlpre, "file:///%s/",
439 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
441 /* urlpre should be URLized.... */
445 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
446 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
448 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
449 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
455 (void) SIGNAL( SIGPIPE, SIG_IGN );
460 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
461 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
465 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
466 perror( "ldap_init" );
471 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
473 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
476 if (timelimit != -1 &&
477 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
479 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
482 if (sizelimit != -1 &&
483 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
485 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
490 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
491 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
493 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
494 referrals ? "on" : "off" );
498 if (version == -1 ) {
502 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
503 != LDAP_OPT_SUCCESS )
505 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
510 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
512 ldap_perror( ld, "ldap_start_tls" );
515 fprintf( stderr, "WARNING: could not start TLS\n" );
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 ldap_set_sasl_interact_proc( ld, lutil_sasl_interact );
527 if( sasl_secprops != NULL ) {
528 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
529 (void *) sasl_secprops );
531 if( rc != LDAP_OPT_SUCCESS ) {
533 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
535 return( EXIT_FAILURE );
539 rc = ldap_sasl_interactive_bind_s( ld, binddn,
540 sasl_mech, NULL, NULL );
542 if( rc != LDAP_SUCCESS ) {
543 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
544 return( EXIT_FAILURE );
547 fprintf( stderr, "%s was not compiled with SASL support\n",
549 return( EXIT_FAILURE );
552 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
554 ldap_perror( ld, "ldap_bind" );
555 return( EXIT_FAILURE );
562 LDAPControl *ctrls[2];
566 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
567 c.ldctl_value.bv_val = NULL;
568 c.ldctl_value.bv_len = 0;
569 c.ldctl_iscritical = manageDSAit > 1;
571 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
573 if( err != LDAP_OPT_SUCCESS ) {
574 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
575 c.ldctl_iscritical ? "critical " : "" );
576 if( c.ldctl_iscritical ) {
577 exit( EXIT_FAILURE );
583 fprintf( stderr, "filter%s: %s\nrequesting: ",
584 infile != NULL ? " pattern" : "",
587 if ( attrs == NULL ) {
588 fprintf( stderr, "ALL" );
590 for ( i = 0; attrs[ i ] != NULL; ++i ) {
591 fprintf( stderr, "%s ", attrs[ i ] );
594 fprintf( stderr, "\n" );
598 printf( "version: %d\n\n", ldif ? 1 : 2 );
602 printf( "#\n# filter%s: %s\n# requesting: ",
603 infile != NULL ? " pattern" : "",
606 if ( attrs == NULL ) {
609 for ( i = 0; attrs[ i ] != NULL; ++i ) {
610 printf( "%s ", attrs[ i ] );
615 printf("\n# with manageDSAit %scontrol",
616 manageDSAit > 1 ? "critical " : "" );
622 if ( infile == NULL ) {
623 rc = dosearch( ld, base, scope, NULL, filtpattern,
624 attrs, attrsonly, NULL, NULL, NULL, -1 );
629 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
630 line[ strlen( line ) - 1 ] = '\0';
636 rc = dosearch( ld, base, scope, filtpattern, line,
637 attrs, attrsonly, NULL, NULL, NULL, -1 );
657 LDAPControl **sctrls,
658 LDAPControl **cctrls,
659 struct timeval *timelimit,
662 char filter[ BUFSIZ ];
669 LDAPMessage *res, *msg;
672 if( filtpatt != NULL ) {
673 sprintf( filter, filtpatt, value );
676 fprintf( stderr, "filter is: (%s)\n", filter );
680 printf( "#\n# filter: %s\n#\n", filter );
684 sprintf( filter, "%s", value );
691 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
692 sctrls, cctrls, timelimit, sizelimit, &msgid );
694 if( rc != LDAP_SUCCESS ) {
695 fprintf( stderr, "ldapsearch: ldap_search_ext: %s (%d)\n",
696 ldap_err2string( rc ), rc );
700 nresponses = nentries = nreferences = nextended = npartial = 0;
704 while ((rc = ldap_result( ld, LDAP_RES_ANY,
705 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
709 (void) ldap_sort_entries( ld, &res,
710 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
713 for ( msg = ldap_first_message( ld, res );
715 msg = ldap_next_message( ld, msg ) )
717 if( nresponses++ ) putchar('\n');
719 switch( ldap_msgtype( msg ) ) {
720 case LDAP_RES_SEARCH_ENTRY:
722 print_entry( ld, msg, attrsonly );
725 case LDAP_RES_SEARCH_REFERENCE:
727 print_reference( ld, msg );
730 case LDAP_RES_EXTENDED:
732 print_extended( ld, msg );
734 if( ldap_msgid( msg ) == 0 ) {
735 /* unsolicited extended operation */
740 case LDAP_RES_EXTENDED_PARTIAL:
742 print_partial( ld, msg );
745 case LDAP_RES_SEARCH_RESULT:
746 rc = print_result( ld, msg, 1 );
755 ldap_perror( ld, "ldap_result" );
761 printf( "\n# numResponses: %d\n", nresponses );
762 if( nentries ) printf( "# numEntries: %d\n", nentries );
763 if( nextended ) printf( "# numExtended: %d\n", nextended );
764 if( npartial ) printf( "# numPartial: %d\n", npartial );
765 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
778 char tmpfname[ 256 ];
781 BerElement *ber = NULL;
782 struct berval **bvals;
783 LDAPControl **ctrls = NULL;
786 dn = ldap_get_dn( ld, entry );
790 ufn = ldap_dn2ufn( dn );
791 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
793 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
795 rc = ldap_get_entry_controls( ld, entry, &ctrls );
797 if( rc != LDAP_SUCCESS ) {
798 fprintf(stderr, "print_entry: %d\n", rc );
799 ldap_perror( ld, "ldap_get_entry_controls" );
800 exit( EXIT_FAILURE );
804 print_ctrls( ctrls );
805 ldap_controls_free( ctrls );
810 ufn = ldap_dn2ufn( dn );
812 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
815 if( ufn != NULL ) ldap_memfree( ufn );
818 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
819 a = ldap_next_attribute( ld, entry, ber ) )
822 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
824 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
825 for ( i = 0; bvals[i] != NULL; i++ ) {
826 if ( vals2tmp > 1 || ( vals2tmp
827 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
830 /* write value to file */
831 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
835 if ( mktemp( tmpfname ) == NULL ) {
840 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
845 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
850 if ( fwrite( bvals[ i ]->bv_val,
851 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
860 sprintf( url, "%s%s", urlpre,
861 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
863 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
866 write_ldif( LDIF_PUT_VALUE, a,
867 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
870 ber_bvecfree( bvals );
879 static void print_reference(
881 LDAPMessage *reference )
888 printf("# search reference\n");
891 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
893 if( rc != LDAP_SUCCESS ) {
894 ldap_perror(ld, "ldap_parse_reference");
895 exit( EXIT_FAILURE );
900 for( i=0; refs[i] != NULL; i++ ) {
901 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
902 "ref", refs[i], strlen(refs[i]) );
904 ber_memvfree( (void **) refs );
908 print_ctrls( ctrls );
909 ldap_controls_free( ctrls );
913 static void print_extended(
915 LDAPMessage *extended )
919 struct berval *retdata = NULL;
922 printf("# extended result response\n");
925 rc = ldap_parse_extended_result( ld, extended,
926 &retoid, &retdata, 0 );
928 if( rc != LDAP_SUCCESS ) {
929 ldap_perror(ld, "ldap_parse_extended_result");
930 exit( EXIT_FAILURE );
933 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
934 "extended", retoid, retoid ? strlen(retoid) : 0 );
935 ber_memfree( retoid );
938 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
939 "data", retdata->bv_val, retdata->bv_len );
940 ber_bvfree( retdata );
943 print_result( ld, extended, 0 );
946 static void print_partial(
948 LDAPMessage *partial )
952 struct berval *retdata = NULL;
953 LDAPControl **ctrls = NULL;
956 printf("# extended partial response\n");
959 rc = ldap_parse_extended_partial( ld, partial,
960 &retoid, &retdata, &ctrls, 0 );
962 if( rc != LDAP_SUCCESS ) {
963 ldap_perror(ld, "ldap_parse_extended_partial");
964 exit( EXIT_FAILURE );
967 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
968 "partial", retoid, retoid ? strlen(retoid) : 0 );
970 ber_memfree( retoid );
973 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
975 retdata->bv_val, retdata->bv_len );
977 ber_bvfree( retdata );
981 print_ctrls( ctrls );
982 ldap_controls_free( ctrls );
986 static int print_result(
988 LDAPMessage *result, int search )
993 char *matcheddn = NULL;
996 LDAPControl **ctrls = NULL;
1000 printf("# search result\n");
1003 printf("%s: %d\n", "search", ldap_msgid(result) );
1007 rc = ldap_parse_result( ld, result,
1008 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1010 if( rc != LDAP_SUCCESS ) {
1011 ldap_perror(ld, "ldap_parse_result");
1012 exit( EXIT_FAILURE );
1017 printf( "result: %d %s\n", err, ldap_err2string(err) );
1019 } else if ( err != LDAP_SUCCESS ) {
1020 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1023 if( matcheddn && *matcheddn ) {
1025 write_ldif( LDIF_PUT_VALUE,
1026 "matchedDN", matcheddn, strlen(matcheddn) );
1028 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1031 ber_memfree( matcheddn );
1034 if( text && *text ) {
1036 write_ldif( LDIF_PUT_TEXT, "text",
1037 text, strlen(text) );
1039 fprintf( stderr, "Additional information: %s\n", text );
1042 ber_memfree( text );
1047 for( i=0; refs[i] != NULL; i++ ) {
1049 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1051 fprintf( stderr, "Referral: %s\n", refs[i] );
1055 ber_memvfree( (void **) refs );
1059 print_ctrls( ctrls );
1060 ldap_controls_free( ctrls );
1066 void print_ctrls( LDAPControl **ctrls ) {
1068 for(i=0; ctrls[i] != NULL; i++ ) {
1069 /* control: OID criticality base64value */
1070 struct berval *b64 = NULL;
1074 len = strlen( ctrls[i]->ldctl_oid );
1076 /* add enough for space after OID and the critical value itself */
1077 len += ctrls[i]->ldctl_iscritical
1078 ? sizeof("true") : sizeof("false");
1080 /* convert to base64 */
1081 if( ctrls[i]->ldctl_value.bv_len ) {
1082 b64 = ber_memalloc( sizeof(struct berval) );
1084 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1085 ctrls[i]->ldctl_value.bv_len ) + 1;
1086 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1088 b64->bv_len = lutil_b64_ntop(
1089 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1090 b64->bv_val, b64->bv_len );
1094 len += 1 + b64->bv_len;
1097 str = malloc( len + 1 );
1098 strcpy( str, ctrls[i]->ldctl_oid );
1099 strcat( str, ctrls[i]->ldctl_iscritical
1100 ? " true" : " false" );
1104 strcat(str, b64->bv_val );
1107 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1108 "control", str, len );
1116 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1120 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1124 fputs( ldif, stdout );
1125 ber_memfree( ldif );