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"
50 "\t-a deref\tdereference aliases: never (default), always, search, or find\n"
51 "\t-A\t\tretrieve attribute names only (no values)\n"
52 "\t-b basedn\tbase dn for search\n"
53 "\t-l limit\ttime limit (in seconds) for search\n"
54 "\t-L\t\tprint responses in LDIFv1 format\n"
55 "\t-LL\t\tprint responses in LDIF format without comments\n"
56 "\t-LLL\t\tprint responses in LDIF format without comments\n"
58 "\t-s scope\tone of base, one, or sub (search scope)\n"
59 "\t-S attr\t\tsort the results by attribute `attr'\n"
60 "\t-t\t\twrite binary values to files in temporary directory\n"
61 "\t-tt\t\twrite all values to files in temporary directory\n"
62 "\t-T path\t\twrite files to directory specified by path (default:\n"
63 "\t\t\t\"" LDAP_TMPDIR "\")\n"
64 "\t-u\t\tinclude User Friendly entry names in the output\n"
67 "\t-d level\tset LDAP debugging level to `level'\n"
68 "\t-D binddn\tbind DN\n"
69 "\t-f file\t\tread operations from `file'\n"
70 "\t-h host\t\tLDAP server\n"
71 "\t-k\t\tuse Kerberos authentication\n"
72 "\t-K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
73 "\t-M\t\tenable Manage DSA IT control (-MM to make critical)\n"
74 "\t-n\t\tshow what would be done but don't actually search\n"
75 "\t-O secprops\tSASL security properties\n"
76 "\t-p port\t\tport on LDAP server\n"
77 "\t-P version\tprocotol version (default: 3)\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\t\tSimple authentication\n"
84 "\t-X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
85 "\t-Y mech\t\tSASL mechanism\n"
86 "\t-z limit\tsize limit (in entries) for search\n"
87 "\t-Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
93 static void print_entry LDAP_P((
98 static void print_reference(
100 LDAPMessage *reference );
102 static void print_extended(
104 LDAPMessage *extended );
106 static void print_partial(
108 LDAPMessage *partial );
110 static int print_result(
115 static void print_ctrls(
116 LDAPControl **ctrls );
118 static int write_ldif LDAP_P((
124 static int dosearch LDAP_P((
132 LDAPControl **sctrls,
133 LDAPControl **cctrls,
134 struct timeval *timelimit,
137 static char *tmpdir = NULL;
138 static char *urlpre = NULL;
140 static char *binddn = NULL;
141 static struct berval passwd = { 0, NULL };
142 static char *base = NULL;
143 static char *ldaphost = NULL;
144 static int ldapport = 0;
145 #ifdef HAVE_CYRUS_SASL
146 static char *sasl_authc_id = NULL;
147 static char *sasl_authz_id = NULL;
148 static char *sasl_mech = NULL;
149 static char *sasl_secprops = NULL;
151 static int use_tls = 0;
152 static char *sortattr = NULL;
153 static int verbose, not, includeufn, vals2tmp, ldif;
156 main( int argc, char **argv )
158 char *prog, *infile, *filtpattern, **attrs, line[BUFSIZ];
160 int rc, i, first, scope, deref, attrsonly, manageDSAit;
161 int referrals, timelimit, sizelimit, debug;
162 int authmethod, version, want_bindpw;
166 debug = verbose = not = vals2tmp = referrals =
167 attrsonly = manageDSAit = ldif = want_bindpw = 0;
169 deref = sizelimit = timelimit = version = -1;
171 scope = LDAP_SCOPE_SUBTREE;
174 while (( i = getopt( argc, argv,
175 "Aa:b:f:Ll:S:s:T:tuV:z:" "Cd:D:h:kKMnO:p:P:U:vw:WxX:Y:Z")) != EOF )
179 case 'a': /* set alias deref option */
180 if ( strcasecmp( optarg, "never" ) == 0 ) {
181 deref = LDAP_DEREF_NEVER;
182 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
183 deref = LDAP_DEREF_SEARCHING;
184 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
185 deref = LDAP_DEREF_FINDING;
186 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
187 deref = LDAP_DEREF_ALWAYS;
189 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
193 case 'A': /* retrieve attribute names only -- no values */
196 case 'f': /* input file */
197 infile = strdup( optarg );
199 case 'l': /* time limit */
200 timelimit = atoi( optarg );
202 case 'L': /* print entries in LDIF format */
205 case 's': /* search scope */
206 if ( strcasecmp( optarg, "base" ) == 0 ) {
207 scope = LDAP_SCOPE_BASE;
208 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
209 scope = LDAP_SCOPE_ONELEVEL;
210 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
211 scope = LDAP_SCOPE_SUBTREE;
213 fprintf( stderr, "scope should be base, one, or sub\n" );
217 case 'S': /* sort attribute */
218 sortattr = strdup( optarg );
220 case 'u': /* include UFN */
223 case 't': /* write attribute values to TMPDIR files */
226 case 'T': /* tmpdir */
227 if( tmpdir ) free( tmpdir );
228 tmpdir = strdup( optarg );
230 case 'V': /* uri prefix */
231 if( urlpre ) free( urlpre );
232 urlpre = strdup( optarg );
234 case 'z': /* size limit */
235 sizelimit = atoi( optarg );
243 debug |= atoi( optarg );
245 case 'D': /* bind DN */
246 binddn = strdup( optarg );
248 case 'h': /* ldap host */
249 ldaphost = strdup( optarg );
251 case 'k': /* kerberos bind */
252 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
253 if( version > LDAP_VERSION2 ) {
254 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
259 if( authmethod != -1 ) {
260 fprintf( stderr, "%s: -k incompatible with previous "
261 "authentication choice\n", prog );
265 authmethod = LDAP_AUTH_KRBV4;
267 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
271 case 'K': /* kerberos bind, part one only */
272 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
273 if( version > LDAP_VERSION2 ) {
274 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
278 if( authmethod != -1 ) {
279 fprintf( stderr, "%s: incompatible with previous "
280 "authentication choice\n", prog );
284 authmethod = LDAP_AUTH_KRBV41;
286 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
287 return( EXIT_FAILURE );
291 /* enable Manage DSA IT */
292 if( version == LDAP_VERSION2 ) {
293 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
298 version = LDAP_VERSION3;
300 case 'n': /* print deletes, don't actually do them */
304 #ifdef HAVE_CYRUS_SASL
305 if( version == LDAP_VERSION2 ) {
306 fprintf( stderr, "%s -O incompatible with LDAPv%d\n",
310 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
311 fprintf( stderr, "%s: incompatible previous "
312 "authentication choice\n", prog );
315 sasl_secprops = strdup( optarg );
316 authmethod = LDAP_AUTH_SASL;
317 version = LDAP_VERSION3;
319 fprintf( stderr, "%s: not compiled with SASL support\n",
321 return( EXIT_FAILURE );
325 ldapport = atoi( optarg );
328 switch( atoi(optarg) ) {
330 if( version == LDAP_VERSION3 ) {
331 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
335 version = LDAP_VERSION2;
338 if( version == LDAP_VERSION2 ) {
339 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
343 version = LDAP_VERSION3;
346 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
349 return( EXIT_FAILURE );
352 #ifdef HAVE_CYRUS_SASL
353 if( version == LDAP_VERSION2 ) {
354 fprintf( stderr, "%s: -U incompatible with version %d\n",
358 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
359 fprintf( stderr, "%s: incompatible previous "
360 "authentication choice\n",
364 authmethod = LDAP_AUTH_SASL;
365 version = LDAP_VERSION3;
367 sasl_authc_id = strdup( optarg );
368 authmethod = LDAP_AUTH_SASL;
370 fprintf( stderr, "%s: was not compiled with SASL support\n",
372 return( EXIT_FAILURE );
375 case 'v': /* verbose mode */
378 case 'w': /* password */
379 passwd.bv_val = strdup( optarg );
383 for( p = optarg; *p == '\0'; p++ ) {
387 passwd.bv_len = strlen( passwd.bv_val );
393 #ifdef HAVE_CYRUS_SASL
394 if( version == LDAP_VERSION2 ) {
395 fprintf( stderr, "%s: -Y incompatible with version %d\n",
399 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
400 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
404 authmethod = LDAP_AUTH_SASL;
405 version = LDAP_VERSION3;
407 fprintf( stderr, "%s: was not compiled with SASL support\n",
409 return( EXIT_FAILURE );
413 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
414 fprintf( stderr, "%s: incompatible with previous "
415 "authentication choice\n", prog );
418 authmethod = LDAP_AUTH_SIMPLE;
421 #ifdef HAVE_CYRUS_SASL
422 if( version == LDAP_VERSION2 ) {
423 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
427 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
428 fprintf( stderr, "%s: -X incompatible with "
429 "authentication choice\n", prog );
432 authmethod = LDAP_AUTH_SASL;
433 version = LDAP_VERSION3;
435 sasl_authz_id = strdup( optarg );
436 authmethod = LDAP_AUTH_SASL;
438 fprintf( stderr, "%s: not compiled with SASL support\n",
440 return( EXIT_FAILURE );
445 if( version == LDAP_VERSION2 ) {
446 fprintf( stderr, "%s -Z incompatible with version %d\n",
450 version = LDAP_VERSION3;
453 fprintf( stderr, "%s: not compiled with TLS support\n",
455 return( EXIT_FAILURE );
464 version = LDAP_VERSION3;
466 if (authmethod == -1 && version > LDAP_VERSION2) {
467 authmethod = LDAP_AUTH_SASL;
470 if (( argc - optind < 1 ) ||
471 ( strchr( argv[optind], '=' ) == NULL ) )
473 filtpattern = "(objectclass=*)";
475 filtpattern = strdup( argv[optind++] );
478 if ( argv[optind] == NULL ) {
480 } else if ( sortattr == NULL || *sortattr == '\0' ) {
481 attrs = &argv[optind];
484 if ( infile != NULL ) {
485 if ( infile[0] == '-' && infile[1] == '\0' ) {
487 } else if (( fp = fopen( infile, "r" )) == NULL ) {
494 && (tmpdir = getenv("TMPDIR")) == NULL
495 && (tmpdir = getenv("TMP")) == NULL
496 && (tmpdir = getenv("TEMP")) == NULL )
498 tmpdir = LDAP_TMPDIR;
501 if( urlpre == NULL ) {
502 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
504 if( urlpre == NULL ) {
509 sprintf( urlpre, "file:///%s/",
510 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
512 /* urlpre should be URLized.... */
516 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
517 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
519 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
520 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
526 (void) SIGNAL( SIGPIPE, SIG_IGN );
531 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
532 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
536 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
537 perror( "ldap_init" );
542 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
544 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
547 if (timelimit != -1 &&
548 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
550 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
553 if (sizelimit != -1 &&
554 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
556 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
561 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
562 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
564 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
565 referrals ? "on" : "off" );
569 if (version == -1 ) {
573 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
574 != LDAP_OPT_SUCCESS )
576 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
581 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
583 ldap_perror( ld, "ldap_start_tls" );
586 fprintf( stderr, "WARNING: could not start TLS\n" );
590 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
591 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
594 if ( authmethod == LDAP_AUTH_SASL ) {
595 #ifdef HAVE_CYRUS_SASL
596 if( sasl_secprops != NULL ) {
597 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
598 (void *) sasl_secprops );
600 if( rc != LDAP_OPT_SUCCESS ) {
602 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
604 return( EXIT_FAILURE );
608 rc = ldap_sasl_interactive_bind_s( ld, binddn,
609 sasl_mech, NULL, NULL, lutil_sasl_interact );
611 if( rc != LDAP_SUCCESS ) {
612 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
613 return( EXIT_FAILURE );
616 fprintf( stderr, "%s was not compiled with SASL support\n",
618 return( EXIT_FAILURE );
621 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
623 ldap_perror( ld, "ldap_bind" );
624 return( EXIT_FAILURE );
631 LDAPControl *ctrls[2];
635 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
636 c.ldctl_value.bv_val = NULL;
637 c.ldctl_value.bv_len = 0;
638 c.ldctl_iscritical = manageDSAit > 1;
640 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
642 if( err != LDAP_OPT_SUCCESS ) {
643 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
644 c.ldctl_iscritical ? "critical " : "" );
645 if( c.ldctl_iscritical ) {
646 exit( EXIT_FAILURE );
652 fprintf( stderr, "filter%s: %s\nrequesting: ",
653 infile != NULL ? " pattern" : "",
656 if ( attrs == NULL ) {
657 fprintf( stderr, "ALL" );
659 for ( i = 0; attrs[ i ] != NULL; ++i ) {
660 fprintf( stderr, "%s ", attrs[ i ] );
663 fprintf( stderr, "\n" );
667 printf( "version: %d\n\n", ldif ? 1 : 2 );
671 printf( "#\n# filter%s: %s\n# requesting: ",
672 infile != NULL ? " pattern" : "",
675 if ( attrs == NULL ) {
678 for ( i = 0; attrs[ i ] != NULL; ++i ) {
679 printf( "%s ", attrs[ i ] );
684 printf("\n# with manageDSAit %scontrol",
685 manageDSAit > 1 ? "critical " : "" );
691 if ( infile == NULL ) {
692 rc = dosearch( ld, base, scope, NULL, filtpattern,
693 attrs, attrsonly, NULL, NULL, NULL, -1 );
698 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
699 line[ strlen( line ) - 1 ] = '\0';
705 rc = dosearch( ld, base, scope, filtpattern, line,
706 attrs, attrsonly, NULL, NULL, NULL, -1 );
726 LDAPControl **sctrls,
727 LDAPControl **cctrls,
728 struct timeval *timelimit,
731 char filter[ BUFSIZ ];
738 LDAPMessage *res, *msg;
741 if( filtpatt != NULL ) {
742 sprintf( filter, filtpatt, value );
745 fprintf( stderr, "filter is: (%s)\n", filter );
749 printf( "#\n# filter: %s\n#\n", filter );
753 sprintf( filter, "%s", value );
760 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
761 sctrls, cctrls, timelimit, sizelimit, &msgid );
763 if( rc != LDAP_SUCCESS ) {
764 fprintf( stderr, "ldapsearch: ldap_search_ext: %s (%d)\n",
765 ldap_err2string( rc ), rc );
769 nresponses = nentries = nreferences = nextended = npartial = 0;
773 while ((rc = ldap_result( ld, LDAP_RES_ANY,
774 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
778 (void) ldap_sort_entries( ld, &res,
779 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
782 for ( msg = ldap_first_message( ld, res );
784 msg = ldap_next_message( ld, msg ) )
786 if( nresponses++ ) putchar('\n');
788 switch( ldap_msgtype( msg ) ) {
789 case LDAP_RES_SEARCH_ENTRY:
791 print_entry( ld, msg, attrsonly );
794 case LDAP_RES_SEARCH_REFERENCE:
796 print_reference( ld, msg );
799 case LDAP_RES_EXTENDED:
801 print_extended( ld, msg );
803 if( ldap_msgid( msg ) == 0 ) {
804 /* unsolicited extended operation */
809 case LDAP_RES_EXTENDED_PARTIAL:
811 print_partial( ld, msg );
814 case LDAP_RES_SEARCH_RESULT:
815 rc = print_result( ld, msg, 1 );
824 ldap_perror( ld, "ldap_result" );
830 printf( "\n# numResponses: %d\n", nresponses );
831 if( nentries ) printf( "# numEntries: %d\n", nentries );
832 if( nextended ) printf( "# numExtended: %d\n", nextended );
833 if( npartial ) printf( "# numPartial: %d\n", npartial );
834 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
847 char tmpfname[ 256 ];
850 BerElement *ber = NULL;
851 struct berval **bvals;
852 LDAPControl **ctrls = NULL;
855 dn = ldap_get_dn( ld, entry );
859 ufn = ldap_dn2ufn( dn );
860 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
862 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
864 rc = ldap_get_entry_controls( ld, entry, &ctrls );
866 if( rc != LDAP_SUCCESS ) {
867 fprintf(stderr, "print_entry: %d\n", rc );
868 ldap_perror( ld, "ldap_get_entry_controls" );
869 exit( EXIT_FAILURE );
873 print_ctrls( ctrls );
874 ldap_controls_free( ctrls );
879 ufn = ldap_dn2ufn( dn );
881 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
884 if( ufn != NULL ) ldap_memfree( ufn );
887 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
888 a = ldap_next_attribute( ld, entry, ber ) )
891 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
893 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
894 for ( i = 0; bvals[i] != NULL; i++ ) {
895 if ( vals2tmp > 1 || ( vals2tmp
896 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
899 /* write value to file */
900 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
904 if ( mktemp( tmpfname ) == NULL ) {
909 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
914 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
919 if ( fwrite( bvals[ i ]->bv_val,
920 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
929 sprintf( url, "%s%s", urlpre,
930 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
932 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
935 write_ldif( LDIF_PUT_VALUE, a,
936 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
939 ber_bvecfree( bvals );
948 static void print_reference(
950 LDAPMessage *reference )
957 printf("# search reference\n");
960 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
962 if( rc != LDAP_SUCCESS ) {
963 ldap_perror(ld, "ldap_parse_reference");
964 exit( EXIT_FAILURE );
969 for( i=0; refs[i] != NULL; i++ ) {
970 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
971 "ref", refs[i], strlen(refs[i]) );
973 ber_memvfree( (void **) refs );
977 print_ctrls( ctrls );
978 ldap_controls_free( ctrls );
982 static void print_extended(
984 LDAPMessage *extended )
988 struct berval *retdata = NULL;
991 printf("# extended result response\n");
994 rc = ldap_parse_extended_result( ld, extended,
995 &retoid, &retdata, 0 );
997 if( rc != LDAP_SUCCESS ) {
998 ldap_perror(ld, "ldap_parse_extended_result");
999 exit( EXIT_FAILURE );
1002 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1003 "extended", retoid, retoid ? strlen(retoid) : 0 );
1004 ber_memfree( retoid );
1007 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1008 "data", retdata->bv_val, retdata->bv_len );
1009 ber_bvfree( retdata );
1012 print_result( ld, extended, 0 );
1015 static void print_partial(
1017 LDAPMessage *partial )
1020 char *retoid = NULL;
1021 struct berval *retdata = NULL;
1022 LDAPControl **ctrls = NULL;
1025 printf("# extended partial response\n");
1028 rc = ldap_parse_extended_partial( ld, partial,
1029 &retoid, &retdata, &ctrls, 0 );
1031 if( rc != LDAP_SUCCESS ) {
1032 ldap_perror(ld, "ldap_parse_extended_partial");
1033 exit( EXIT_FAILURE );
1036 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1037 "partial", retoid, retoid ? strlen(retoid) : 0 );
1039 ber_memfree( retoid );
1042 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1044 retdata->bv_val, retdata->bv_len );
1046 ber_bvfree( retdata );
1050 print_ctrls( ctrls );
1051 ldap_controls_free( ctrls );
1055 static int print_result(
1057 LDAPMessage *result, int search )
1062 char *matcheddn = NULL;
1065 LDAPControl **ctrls = NULL;
1069 printf("# search result\n");
1072 printf("%s: %d\n", "search", ldap_msgid(result) );
1076 rc = ldap_parse_result( ld, result,
1077 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1079 if( rc != LDAP_SUCCESS ) {
1080 ldap_perror(ld, "ldap_parse_result");
1081 exit( EXIT_FAILURE );
1086 printf( "result: %d %s\n", err, ldap_err2string(err) );
1088 } else if ( err != LDAP_SUCCESS ) {
1089 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1092 if( matcheddn && *matcheddn ) {
1094 write_ldif( LDIF_PUT_VALUE,
1095 "matchedDN", matcheddn, strlen(matcheddn) );
1097 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1100 ber_memfree( matcheddn );
1103 if( text && *text ) {
1105 write_ldif( LDIF_PUT_TEXT, "text",
1106 text, strlen(text) );
1108 fprintf( stderr, "Additional information: %s\n", text );
1111 ber_memfree( text );
1116 for( i=0; refs[i] != NULL; i++ ) {
1118 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1120 fprintf( stderr, "Referral: %s\n", refs[i] );
1124 ber_memvfree( (void **) refs );
1128 print_ctrls( ctrls );
1129 ldap_controls_free( ctrls );
1135 void print_ctrls( LDAPControl **ctrls ) {
1137 for(i=0; ctrls[i] != NULL; i++ ) {
1138 /* control: OID criticality base64value */
1139 struct berval *b64 = NULL;
1143 len = strlen( ctrls[i]->ldctl_oid );
1145 /* add enough for space after OID and the critical value itself */
1146 len += ctrls[i]->ldctl_iscritical
1147 ? sizeof("true") : sizeof("false");
1149 /* convert to base64 */
1150 if( ctrls[i]->ldctl_value.bv_len ) {
1151 b64 = ber_memalloc( sizeof(struct berval) );
1153 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1154 ctrls[i]->ldctl_value.bv_len ) + 1;
1155 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1157 b64->bv_len = lutil_b64_ntop(
1158 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1159 b64->bv_val, b64->bv_len );
1163 len += 1 + b64->bv_len;
1166 str = malloc( len + 1 );
1167 strcpy( str, ctrls[i]->ldctl_oid );
1168 strcat( str, ctrls[i]->ldctl_iscritical
1169 ? " true" : " false" );
1173 strcat(str, b64->bv_val );
1176 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1177 "control", str, len );
1185 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1189 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1193 fputs( ldif, stdout );
1194 ber_memfree( ldif );