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-s scope\tone of base, one, or sub (search scope)\n"
70 "\t-S attr\t\tsort the results by attribute `attr'\n"
71 "\t-t\t\twrite binary values to files in temporary directory\n"
72 "\t-tt\t\twrite all values to files in temporary directory\n"
73 "\t-T path\t\twrite files to directory specified by path (default:\n"
74 "\t\t\t\"" LDAP_TMPDIR "\")\n"
75 "\t-u\t\tinclude User Friendly entry names in the output\n"
76 "\t-U user\t\tSASL authentication identity (username)\n"
77 "\t-v\t\trun in verbose mode (diagnostics to standard output)\n"
78 "\t-V prefix\tURL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
79 "\t-w passwd\tbind passwd (for simple authentication)\n"
80 "\t-W\t\tprompt for bind passwd\n"
81 "\t-X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
82 "\t-Y mech\t\tSASL mechanism\n"
83 "\t-z limit\tsize limit (in entries) for search\n"
84 "\t-Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
90 static void print_entry LDAP_P((
95 static void print_reference(
97 LDAPMessage *reference );
99 static void print_extended(
101 LDAPMessage *extended );
103 static void print_partial(
105 LDAPMessage *partial );
107 static int print_result(
112 static void print_ctrls(
113 LDAPControl **ctrls );
115 static int write_ldif LDAP_P((
121 static int dosearch LDAP_P((
129 LDAPControl **sctrls,
130 LDAPControl **cctrls,
131 struct timeval *timelimit,
134 static char *tmpdir = NULL;
135 static char *urlpre = NULL;
137 static char *binddn = NULL;
138 static struct berval passwd = { 0, NULL };
139 static char *base = NULL;
140 static char *ldaphost = NULL;
141 static int ldapport = 0;
142 #ifdef HAVE_CYRUS_SASL
143 static char *sasl_authc_id = NULL;
144 static char *sasl_authz_id = NULL;
145 static char *sasl_mech = NULL;
146 static int sasl_integrity = 0;
147 static int sasl_privacy = 0;
149 static int use_tls = 0;
150 static char *sortattr = NULL;
151 static int verbose, not, includeufn, vals2tmp, ldif;
154 main( int argc, char **argv )
156 char *infile, *filtpattern, **attrs, line[BUFSIZ];
158 int rc, i, first, scope, deref, attrsonly, manageDSAit;
159 int referrals, timelimit, sizelimit, debug;
160 int authmethod, version, want_bindpw;
164 debug = verbose = not = vals2tmp = referrals =
165 attrsonly = manageDSAit = ldif = want_bindpw = 0;
167 deref = sizelimit = timelimit = version = -1;
169 scope = LDAP_SCOPE_SUBTREE;
170 authmethod = LDAP_AUTH_SIMPLE;
172 while (( i = getopt( argc, argv,
173 "Aa:b:CD: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 */
215 case 'R': /* ignore */
217 case 'A': /* retrieve attribute names only -- no values */
220 case 'L': /* print entries in LDIF format */
224 case 's': /* search scope */
225 if ( strcasecmp( optarg, "base" ) == 0 ) {
226 scope = LDAP_SCOPE_BASE;
227 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
228 scope = LDAP_SCOPE_ONELEVEL;
229 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
230 scope = LDAP_SCOPE_SUBTREE;
232 fprintf( stderr, "scope should be base, one, or sub\n" );
237 case 'a': /* set alias deref option */
238 if ( strcasecmp( optarg, "never" ) == 0 ) {
239 deref = LDAP_DEREF_NEVER;
240 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
241 deref = LDAP_DEREF_SEARCHING;
242 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
243 deref = LDAP_DEREF_FINDING;
244 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
245 deref = LDAP_DEREF_ALWAYS;
247 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
252 case 'T': /* tmpdir */
253 if( tmpdir ) free( tmpdir );
254 tmpdir = strdup( optarg );
256 case 'V': /* uri prefix */
257 if( urlpre ) free( urlpre );
258 urlpre = strdup( optarg );
260 case 'f': /* input file */
261 infile = strdup( optarg );
263 case 'h': /* ldap host */
264 ldaphost = strdup( optarg );
266 case 'b': /* search base */
267 base = strdup( optarg );
269 case 'D': /* bind DN */
270 binddn = strdup( optarg );
272 case 'p': /* ldap port */
273 ldapport = atoi( optarg );
275 case 'w': /* bind password */
276 passwd.bv_val = strdup( optarg );
280 for( p = optarg; *p == '\0'; p++ ) {
284 passwd.bv_len = strlen( passwd.bv_val );
286 case 'l': /* time limit */
287 timelimit = atoi( optarg );
289 case 'z': /* size limit */
290 sizelimit = atoi( optarg );
292 case 'S': /* sort attribute */
293 sortattr = strdup( optarg );
299 switch( atoi( optarg ) )
302 version = LDAP_VERSION2;
305 version = LDAP_VERSION3;
308 fprintf( stderr, "protocol version should be 2 or 3\n" );
313 #ifdef HAVE_CYRUS_SASL
315 authmethod = LDAP_AUTH_SASL;
317 fprintf( stderr, "%s was not compiled with SASL support\n",
319 return( EXIT_FAILURE );
323 #ifdef HAVE_CYRUS_SASL
325 authmethod = LDAP_AUTH_SASL;
327 fprintf( stderr, "%s was not compiled with SASL support\n",
329 return( EXIT_FAILURE );
333 #ifdef HAVE_CYRUS_SASL
334 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
335 sasl_mech = strdup( optarg );
337 authmethod = LDAP_AUTH_SASL;
339 fprintf( stderr, "%s was not compiled with SASL support\n",
341 return( EXIT_FAILURE );
345 #ifdef HAVE_CYRUS_SASL
346 sasl_authc_id = strdup( optarg );
347 authmethod = LDAP_AUTH_SASL;
349 fprintf( stderr, "%s was not compiled with SASL support\n",
351 return( EXIT_FAILURE );
355 #ifdef HAVE_CYRUS_SASL
356 sasl_authz_id = strdup( optarg );
357 authmethod = LDAP_AUTH_SASL;
359 fprintf( stderr, "%s was not compiled with SASL support\n",
361 return( EXIT_FAILURE );
368 fprintf( stderr, "%s was not compiled with TLS support\n",
370 return( EXIT_FAILURE );
378 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
379 LDAP_AUTH_KRBV41 ) ) {
380 if( version > LDAP_VERSION2 ) {
381 fprintf( stderr, "Kerberos requires LDAPv2\n" );
382 return( EXIT_FAILURE );
384 version = LDAP_VERSION2;
386 else if ( authmethod == LDAP_AUTH_SASL ) {
387 if( version != -1 && version != LDAP_VERSION3 ) {
388 fprintf( stderr, "SASL requires LDAPv3\n" );
389 return( EXIT_FAILURE );
391 version = LDAP_VERSION3;
395 if( version != -1 && version != LDAP_VERSION3 ) {
396 fprintf(stderr, "manage DSA control requires LDAPv3\n");
399 version = LDAP_VERSION3;
403 if( version != -1 && version != LDAP_VERSION3 ) {
404 fprintf(stderr, "Start TLS requires LDAPv3\n");
407 version = LDAP_VERSION3;
410 if ( argc - optind < 1 ) {
411 filtpattern = "(objectclass=*)";
413 filtpattern = strdup( argv[optind++] );
416 if ( argv[optind] == NULL ) {
418 } else if ( sortattr == NULL || *sortattr == '\0' ) {
419 attrs = &argv[optind];
422 if ( infile != NULL ) {
423 if ( infile[0] == '-' && infile[1] == '\0' ) {
425 } else if (( fp = fopen( infile, "r" )) == NULL ) {
432 && (tmpdir = getenv("TMPDIR")) == NULL
433 && (tmpdir = getenv("TMP")) == NULL
434 && (tmpdir = getenv("TEMP")) == NULL )
436 tmpdir = LDAP_TMPDIR;
439 if( urlpre == NULL ) {
440 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
442 if( urlpre == NULL ) {
447 sprintf( urlpre, "file:///%s/",
448 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
450 /* urlpre should be URLized.... */
454 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
455 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
457 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
458 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
464 (void) SIGNAL( SIGPIPE, SIG_IGN );
469 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
470 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
474 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
475 perror( "ldap_init" );
480 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
482 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
485 if (timelimit != -1 &&
486 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
488 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
491 if (sizelimit != -1 &&
492 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
494 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
499 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
500 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
502 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
503 referrals ? "on" : "off" );
507 if (version == -1 ) {
511 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
512 != LDAP_OPT_SUCCESS )
514 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
519 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
521 ldap_perror( ld, "ldap_start_tls" );
524 fprintf( stderr, "WARNING: could not start TLS\n" );
528 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
529 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
532 if ( authmethod == LDAP_AUTH_SASL ) {
533 #ifdef HAVE_CYRUS_SASL
534 int minssf = 0, maxssf = 0;
536 if ( sasl_integrity > 0 )
538 if ( sasl_integrity > 1 )
540 if ( sasl_privacy > 0 )
541 maxssf = 100000; /* Something big value */
542 if ( sasl_privacy > 1 )
545 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
546 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
547 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
549 return( EXIT_FAILURE );
551 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
552 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
553 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
555 return( EXIT_FAILURE );
558 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
559 sasl_authz_id, sasl_mech,
560 passwd.bv_len ? &passwd : NULL,
563 if( rc != LDAP_SUCCESS ) {
564 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
565 return( EXIT_FAILURE );
568 fprintf( stderr, "%s was not compiled with SASL support\n",
570 return( EXIT_FAILURE );
573 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
575 ldap_perror( ld, "ldap_bind" );
576 return( EXIT_FAILURE );
583 LDAPControl *ctrls[2];
587 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
588 c.ldctl_value.bv_val = NULL;
589 c.ldctl_value.bv_len = 0;
590 c.ldctl_iscritical = manageDSAit > 1;
592 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
594 if( err != LDAP_OPT_SUCCESS ) {
595 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
596 c.ldctl_iscritical ? "critical " : "" );
597 if( c.ldctl_iscritical ) {
598 exit( EXIT_FAILURE );
604 fprintf( stderr, "filter%s: %s\nrequesting: ",
605 infile != NULL ? " pattern" : "",
608 if ( attrs == NULL ) {
609 fprintf( stderr, "ALL" );
611 for ( i = 0; attrs[ i ] != NULL; ++i ) {
612 fprintf( stderr, "%s ", attrs[ i ] );
615 fprintf( stderr, "\n" );
619 printf( "version: %d\n\n", ldif ? 1 : 2 );
623 printf( "#\n# filter%s: %s\n# requesting: ",
624 infile != NULL ? " pattern" : "",
627 if ( attrs == NULL ) {
630 for ( i = 0; attrs[ i ] != NULL; ++i ) {
631 printf( "%s ", attrs[ i ] );
636 printf("\n# with manageDSAit %scontrol",
637 manageDSAit > 1 ? "critical " : "" );
643 if ( infile == NULL ) {
644 rc = dosearch( ld, base, scope, NULL, filtpattern,
645 attrs, attrsonly, NULL, NULL, NULL, -1 );
650 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
651 line[ strlen( line ) - 1 ] = '\0';
657 rc = dosearch( ld, base, scope, filtpattern, line,
658 attrs, attrsonly, NULL, NULL, NULL, -1 );
678 LDAPControl **sctrls,
679 LDAPControl **cctrls,
680 struct timeval *timelimit,
683 char filter[ BUFSIZ ];
690 LDAPMessage *res, *msg;
693 if( filtpatt != NULL ) {
694 sprintf( filter, filtpatt, value );
697 fprintf( stderr, "filter is: (%s)\n", filter );
701 printf( "#\n# filter: %s\n#\n", filter );
705 sprintf( filter, "%s", value );
712 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
713 sctrls, cctrls, timelimit, sizelimit, &msgid );
715 if( rc != LDAP_SUCCESS ) {
716 ldap_perror( ld, "ldap_search" );
720 nresponses = nentries = nreferences = nextended = npartial = 0;
723 while ((rc = ldap_result( ld, LDAP_RES_ANY,
724 sortattr ? 1 : 0, NULL, &res )) > 0 )
727 (void) ldap_sort_entries( ld, &res,
728 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
731 for ( msg = ldap_first_message( ld, res );
733 msg = ldap_next_message( ld, msg ) )
735 if( nresponses++ ) putchar('\n');
737 switch( ldap_msgtype( msg ) ) {
738 case LDAP_RES_SEARCH_ENTRY:
740 print_entry( ld, msg, attrsonly );
743 case LDAP_RES_SEARCH_REFERENCE:
745 print_reference( ld, msg );
748 case LDAP_RES_EXTENDED:
750 print_extended( ld, msg );
752 if( ldap_msgid( msg ) == 0 ) {
753 /* unsolicited extended operation */
758 case LDAP_RES_EXTENDED_PARTIAL:
760 print_partial( ld, msg );
763 case LDAP_RES_SEARCH_RESULT:
764 rc = print_result( ld, msg, 1 );
773 ldap_perror( ld, "ldap_result" );
779 printf( "\n# numResponses: %d\n", nresponses );
780 if( nentries ) printf( "# numEntries: %d\n", nentries );
781 if( nextended ) printf( "# numExtended: %d\n", nextended );
782 if( npartial ) printf( "# numPartial: %d\n", npartial );
783 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
796 char tmpfname[ 256 ];
799 BerElement *ber = NULL;
800 struct berval **bvals;
801 LDAPControl **ctrls = NULL;
804 dn = ldap_get_dn( ld, entry );
808 ufn = ldap_dn2ufn( dn );
809 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
811 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
813 rc = ldap_get_entry_controls( ld, entry, &ctrls );
815 if( rc != LDAP_SUCCESS ) {
816 fprintf(stderr, "print_entry: %d\n", rc );
817 ldap_perror( ld, "ldap_get_entry_controls" );
818 exit( EXIT_FAILURE );
822 print_ctrls( ctrls );
823 ldap_controls_free( ctrls );
828 ufn = ldap_dn2ufn( dn );
830 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
833 if( ufn != NULL ) ldap_memfree( ufn );
836 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
837 a = ldap_next_attribute( ld, entry, ber ) )
840 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
842 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
843 for ( i = 0; bvals[i] != NULL; i++ ) {
844 if ( vals2tmp > 1 || ( vals2tmp
845 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
848 /* write value to file */
849 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
853 if ( mktemp( tmpfname ) == NULL ) {
858 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
863 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
868 if ( fwrite( bvals[ i ]->bv_val,
869 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
878 sprintf( url, "%s%s", urlpre,
879 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
881 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
884 write_ldif( LDIF_PUT_VALUE, a,
885 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
888 ber_bvecfree( bvals );
897 static void print_reference(
899 LDAPMessage *reference )
906 printf("# search reference\n");
909 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
911 if( rc != LDAP_SUCCESS ) {
912 ldap_perror(ld, "ldap_parse_reference");
913 exit( EXIT_FAILURE );
918 for( i=0; refs[i] != NULL; i++ ) {
919 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
920 "ref", refs[i], strlen(refs[i]) );
922 ber_memvfree( (void **) refs );
926 print_ctrls( ctrls );
927 ldap_controls_free( ctrls );
931 static void print_extended(
933 LDAPMessage *extended )
937 struct berval *retdata = NULL;
940 printf("# extended result response\n");
943 rc = ldap_parse_extended_result( ld, extended,
944 &retoid, &retdata, 0 );
946 if( rc != LDAP_SUCCESS ) {
947 ldap_perror(ld, "ldap_parse_extended_result");
948 exit( EXIT_FAILURE );
951 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
952 "extended", retoid, retoid ? strlen(retoid) : 0 );
953 ber_memfree( retoid );
956 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
957 "data", retdata->bv_val, retdata->bv_len );
958 ber_bvfree( retdata );
961 print_result( ld, extended, 0 );
964 static void print_partial(
966 LDAPMessage *partial )
970 struct berval *retdata = NULL;
971 LDAPControl **ctrls = NULL;
974 printf("# extended partial response\n");
977 rc = ldap_parse_extended_partial( ld, partial,
978 &retoid, &retdata, &ctrls, 0 );
980 if( rc != LDAP_SUCCESS ) {
981 ldap_perror(ld, "ldap_parse_extended_partial");
982 exit( EXIT_FAILURE );
985 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
986 "partial", retoid, retoid ? strlen(retoid) : 0 );
988 ber_memfree( retoid );
991 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
993 retdata->bv_val, retdata->bv_len );
995 ber_bvfree( retdata );
999 print_ctrls( ctrls );
1000 ldap_controls_free( ctrls );
1004 static int print_result(
1006 LDAPMessage *result, int search )
1011 char *matcheddn = NULL;
1014 LDAPControl **ctrls = NULL;
1018 printf("# search result\n");
1021 printf("%s: %d\n", "search", ldap_msgid(result) );
1025 rc = ldap_parse_result( ld, result,
1026 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1028 if( rc != LDAP_SUCCESS ) {
1029 ldap_perror(ld, "ldap_parse_result");
1030 exit( EXIT_FAILURE );
1035 printf( "result: %d %s\n", err, ldap_err2string(err) );
1037 } else if ( err != LDAP_SUCCESS ) {
1038 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1041 if( matcheddn && *matcheddn ) {
1043 write_ldif( LDIF_PUT_VALUE,
1044 "matchedDN", matcheddn, strlen(matcheddn) );
1046 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1049 ber_memfree( matcheddn );
1052 if( text && *text ) {
1054 write_ldif( LDIF_PUT_TEXT, "text",
1055 text, strlen(text) );
1057 fprintf( stderr, "Additional information: %s\n", text );
1060 ber_memfree( text );
1065 for( i=0; refs[i] != NULL; i++ ) {
1067 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1069 fprintf( stderr, "Referral: %s", refs[i] );
1073 ber_memvfree( (void **) refs );
1077 print_ctrls( ctrls );
1078 ldap_controls_free( ctrls );
1084 void print_ctrls( LDAPControl **ctrls ) {
1086 for(i=0; ctrls[i] != NULL; i++ ) {
1087 /* control: OID criticality base64value */
1088 struct berval *b64 = NULL;
1092 len = strlen( ctrls[i]->ldctl_oid );
1094 /* add enough for space after OID and the critical value itself */
1095 len += ctrls[i]->ldctl_iscritical
1096 ? sizeof("true") : sizeof("false");
1098 /* convert to base64 */
1099 if( ctrls[i]->ldctl_value.bv_len ) {
1100 b64 = ber_memalloc( sizeof(struct berval) );
1102 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1103 ctrls[i]->ldctl_value.bv_len ) + 1;
1104 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1106 b64->bv_len = lutil_b64_ntop(
1107 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1108 b64->bv_val, b64->bv_len );
1112 len += 1 + b64->bv_len;
1115 str = malloc( len + 1 );
1116 strcpy( str, ctrls[i]->ldctl_oid );
1117 strcat( str, ctrls[i]->ldctl_iscritical
1118 ? " true" : " false" );
1122 strcat(str, b64->bv_val );
1125 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1126 "control", str, len );
1134 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1138 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1142 fputs( ldif, stdout );
1143 ber_memfree( ldif );