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 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
176 while (( i = getopt( argc, argv,
177 "Aa:b:f:Ll:S:s:T:tuV:z:" "Cd:D:h:kKMnO:p:P:U:vw:WxX:Y:Z")) != EOF )
181 case 'a': /* set alias deref option */
182 if ( strcasecmp( optarg, "never" ) == 0 ) {
183 deref = LDAP_DEREF_NEVER;
184 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
185 deref = LDAP_DEREF_SEARCHING;
186 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
187 deref = LDAP_DEREF_FINDING;
188 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
189 deref = LDAP_DEREF_ALWAYS;
191 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
195 case 'A': /* retrieve attribute names only -- no values */
198 case 'b': /* search base */
199 base = strdup( optarg );
201 case 'f': /* input file */
202 infile = strdup( optarg );
204 case 'l': /* time limit */
205 timelimit = atoi( optarg );
207 case 'L': /* print entries in LDIF format */
210 case 's': /* search scope */
211 if ( strcasecmp( optarg, "base" ) == 0 ) {
212 scope = LDAP_SCOPE_BASE;
213 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
214 scope = LDAP_SCOPE_ONELEVEL;
215 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
216 scope = LDAP_SCOPE_SUBTREE;
218 fprintf( stderr, "scope should be base, one, or sub\n" );
222 case 'S': /* sort attribute */
223 sortattr = strdup( optarg );
225 case 'u': /* include UFN */
228 case 't': /* write attribute values to TMPDIR files */
231 case 'T': /* tmpdir */
232 if( tmpdir ) free( tmpdir );
233 tmpdir = strdup( optarg );
235 case 'V': /* uri prefix */
236 if( urlpre ) free( urlpre );
237 urlpre = strdup( optarg );
239 case 'z': /* size limit */
240 sizelimit = atoi( optarg );
248 debug |= atoi( optarg );
250 case 'D': /* bind DN */
251 binddn = strdup( optarg );
253 case 'h': /* ldap host */
254 ldaphost = strdup( optarg );
256 case 'k': /* kerberos bind */
257 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
258 if( version > LDAP_VERSION2 ) {
259 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
264 if( authmethod != -1 ) {
265 fprintf( stderr, "%s: -k incompatible with previous "
266 "authentication choice\n", prog );
270 authmethod = LDAP_AUTH_KRBV4;
272 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
276 case 'K': /* kerberos bind, part one only */
277 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
278 if( version > LDAP_VERSION2 ) {
279 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
283 if( authmethod != -1 ) {
284 fprintf( stderr, "%s: incompatible with previous "
285 "authentication choice\n", prog );
289 authmethod = LDAP_AUTH_KRBV41;
291 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
292 return( EXIT_FAILURE );
296 /* enable Manage DSA IT */
297 if( version == LDAP_VERSION2 ) {
298 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
303 version = LDAP_VERSION3;
305 case 'n': /* print deletes, don't actually do them */
309 #ifdef HAVE_CYRUS_SASL
310 if( version == LDAP_VERSION2 ) {
311 fprintf( stderr, "%s -O incompatible with LDAPv%d\n",
315 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
316 fprintf( stderr, "%s: incompatible previous "
317 "authentication choice\n", prog );
320 sasl_secprops = strdup( optarg );
321 authmethod = LDAP_AUTH_SASL;
322 version = LDAP_VERSION3;
324 fprintf( stderr, "%s: not compiled with SASL support\n",
326 return( EXIT_FAILURE );
330 ldapport = atoi( optarg );
333 switch( atoi(optarg) ) {
335 if( version == LDAP_VERSION3 ) {
336 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
340 version = LDAP_VERSION2;
343 if( version == LDAP_VERSION2 ) {
344 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
348 version = LDAP_VERSION3;
351 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
354 return( EXIT_FAILURE );
357 #ifdef HAVE_CYRUS_SASL
358 if( version == LDAP_VERSION2 ) {
359 fprintf( stderr, "%s: -U incompatible with version %d\n",
363 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
364 fprintf( stderr, "%s: incompatible previous "
365 "authentication choice\n",
369 authmethod = LDAP_AUTH_SASL;
370 version = LDAP_VERSION3;
372 sasl_authc_id = strdup( optarg );
373 authmethod = LDAP_AUTH_SASL;
375 fprintf( stderr, "%s: was not compiled with SASL support\n",
377 return( EXIT_FAILURE );
380 case 'v': /* verbose mode */
383 case 'w': /* password */
384 passwd.bv_val = strdup( optarg );
388 for( p = optarg; *p == '\0'; p++ ) {
392 passwd.bv_len = strlen( passwd.bv_val );
398 #ifdef HAVE_CYRUS_SASL
399 if( version == LDAP_VERSION2 ) {
400 fprintf( stderr, "%s: -Y incompatible with version %d\n",
404 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
405 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
409 authmethod = LDAP_AUTH_SASL;
410 version = LDAP_VERSION3;
412 fprintf( stderr, "%s: was not compiled with SASL support\n",
414 return( EXIT_FAILURE );
418 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
419 fprintf( stderr, "%s: incompatible with previous "
420 "authentication choice\n", prog );
423 authmethod = LDAP_AUTH_SIMPLE;
426 #ifdef HAVE_CYRUS_SASL
427 if( version == LDAP_VERSION2 ) {
428 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
432 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
433 fprintf( stderr, "%s: -X incompatible with "
434 "authentication choice\n", prog );
437 authmethod = LDAP_AUTH_SASL;
438 version = LDAP_VERSION3;
440 sasl_authz_id = strdup( optarg );
441 authmethod = LDAP_AUTH_SASL;
443 fprintf( stderr, "%s: not compiled with SASL support\n",
445 return( EXIT_FAILURE );
450 if( version == LDAP_VERSION2 ) {
451 fprintf( stderr, "%s -Z incompatible with version %d\n",
455 version = LDAP_VERSION3;
458 fprintf( stderr, "%s: not compiled with TLS support\n",
460 return( EXIT_FAILURE );
464 fprintf( stderr, "%s: unrecongized option -%c\n",
471 version = LDAP_VERSION3;
473 if (authmethod == -1 && version > LDAP_VERSION2) {
474 #ifdef HAVE_CYRUS_SASL
475 authmethod = LDAP_AUTH_SASL;
477 authmethod = LDAP_AUTH_SIMPLE;
481 if (( argc - optind < 1 ) ||
482 ( strchr( argv[optind], '=' ) == NULL ) )
484 filtpattern = "(objectclass=*)";
486 filtpattern = strdup( argv[optind++] );
489 if ( argv[optind] == NULL ) {
491 } else if ( sortattr == NULL || *sortattr == '\0' ) {
492 attrs = &argv[optind];
495 if ( infile != NULL ) {
496 if ( infile[0] == '-' && infile[1] == '\0' ) {
498 } else if (( fp = fopen( infile, "r" )) == NULL ) {
505 && (tmpdir = getenv("TMPDIR")) == NULL
506 && (tmpdir = getenv("TMP")) == NULL
507 && (tmpdir = getenv("TEMP")) == NULL )
509 tmpdir = LDAP_TMPDIR;
512 if( urlpre == NULL ) {
513 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
515 if( urlpre == NULL ) {
520 sprintf( urlpre, "file:///%s/",
521 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
523 /* urlpre should be URLized.... */
527 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
528 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
530 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
531 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
537 (void) SIGNAL( SIGPIPE, SIG_IGN );
542 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
543 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
547 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
548 perror( "ldap_init" );
553 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
555 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
558 if (timelimit != -1 &&
559 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
561 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
564 if (sizelimit != -1 &&
565 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
567 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
572 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
573 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
575 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
576 referrals ? "on" : "off" );
580 if (version == -1 ) {
584 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
585 != LDAP_OPT_SUCCESS )
587 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
592 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
594 ldap_perror( ld, "ldap_start_tls" );
597 fprintf( stderr, "WARNING: could not start TLS\n" );
601 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
602 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
605 if ( authmethod == LDAP_AUTH_SASL ) {
606 #ifdef HAVE_CYRUS_SASL
607 if( sasl_secprops != NULL ) {
608 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
609 (void *) sasl_secprops );
611 if( rc != LDAP_OPT_SUCCESS ) {
613 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
615 return( EXIT_FAILURE );
619 rc = ldap_sasl_interactive_bind_s( ld, binddn,
620 sasl_mech, NULL, NULL, lutil_sasl_interact );
622 if( rc != LDAP_SUCCESS ) {
623 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
624 return( EXIT_FAILURE );
627 fprintf( stderr, "%s was not compiled with SASL support\n",
629 return( EXIT_FAILURE );
632 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
634 ldap_perror( ld, "ldap_bind" );
635 return( EXIT_FAILURE );
642 LDAPControl *ctrls[2];
646 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
647 c.ldctl_value.bv_val = NULL;
648 c.ldctl_value.bv_len = 0;
649 c.ldctl_iscritical = manageDSAit > 1;
651 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
653 if( err != LDAP_OPT_SUCCESS ) {
654 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
655 c.ldctl_iscritical ? "critical " : "" );
656 if( c.ldctl_iscritical ) {
657 exit( EXIT_FAILURE );
663 fprintf( stderr, "filter%s: %s\nrequesting: ",
664 infile != NULL ? " pattern" : "",
667 if ( attrs == NULL ) {
668 fprintf( stderr, "ALL" );
670 for ( i = 0; attrs[ i ] != NULL; ++i ) {
671 fprintf( stderr, "%s ", attrs[ i ] );
674 fprintf( stderr, "\n" );
678 printf( "version: %d\n\n", ldif ? 1 : 2 );
682 printf( "#\n# filter%s: %s\n# requesting: ",
683 infile != NULL ? " pattern" : "",
686 if ( attrs == NULL ) {
689 for ( i = 0; attrs[ i ] != NULL; ++i ) {
690 printf( "%s ", attrs[ i ] );
695 printf("\n# with manageDSAit %scontrol",
696 manageDSAit > 1 ? "critical " : "" );
702 if ( infile == NULL ) {
703 rc = dosearch( ld, base, scope, NULL, filtpattern,
704 attrs, attrsonly, NULL, NULL, NULL, -1 );
709 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
710 line[ strlen( line ) - 1 ] = '\0';
716 rc = dosearch( ld, base, scope, filtpattern, line,
717 attrs, attrsonly, NULL, NULL, NULL, -1 );
737 LDAPControl **sctrls,
738 LDAPControl **cctrls,
739 struct timeval *timelimit,
742 char filter[ BUFSIZ ];
749 LDAPMessage *res, *msg;
752 if( filtpatt != NULL ) {
753 sprintf( filter, filtpatt, value );
756 fprintf( stderr, "filter is: (%s)\n", filter );
760 printf( "#\n# filter: %s\n#\n", filter );
764 sprintf( filter, "%s", value );
771 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
772 sctrls, cctrls, timelimit, sizelimit, &msgid );
774 if( rc != LDAP_SUCCESS ) {
775 fprintf( stderr, "ldapsearch: ldap_search_ext: %s (%d)\n",
776 ldap_err2string( rc ), rc );
780 nresponses = nentries = nreferences = nextended = npartial = 0;
784 while ((rc = ldap_result( ld, LDAP_RES_ANY,
785 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
789 (void) ldap_sort_entries( ld, &res,
790 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
793 for ( msg = ldap_first_message( ld, res );
795 msg = ldap_next_message( ld, msg ) )
797 if( nresponses++ ) putchar('\n');
799 switch( ldap_msgtype( msg ) ) {
800 case LDAP_RES_SEARCH_ENTRY:
802 print_entry( ld, msg, attrsonly );
805 case LDAP_RES_SEARCH_REFERENCE:
807 print_reference( ld, msg );
810 case LDAP_RES_EXTENDED:
812 print_extended( ld, msg );
814 if( ldap_msgid( msg ) == 0 ) {
815 /* unsolicited extended operation */
820 case LDAP_RES_EXTENDED_PARTIAL:
822 print_partial( ld, msg );
825 case LDAP_RES_SEARCH_RESULT:
826 rc = print_result( ld, msg, 1 );
835 ldap_perror( ld, "ldap_result" );
841 printf( "\n# numResponses: %d\n", nresponses );
842 if( nentries ) printf( "# numEntries: %d\n", nentries );
843 if( nextended ) printf( "# numExtended: %d\n", nextended );
844 if( npartial ) printf( "# numPartial: %d\n", npartial );
845 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
858 char tmpfname[ 256 ];
861 BerElement *ber = NULL;
862 struct berval **bvals;
863 LDAPControl **ctrls = NULL;
866 dn = ldap_get_dn( ld, entry );
870 ufn = ldap_dn2ufn( dn );
871 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
873 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
875 rc = ldap_get_entry_controls( ld, entry, &ctrls );
877 if( rc != LDAP_SUCCESS ) {
878 fprintf(stderr, "print_entry: %d\n", rc );
879 ldap_perror( ld, "ldap_get_entry_controls" );
880 exit( EXIT_FAILURE );
884 print_ctrls( ctrls );
885 ldap_controls_free( ctrls );
890 ufn = ldap_dn2ufn( dn );
892 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
895 if( ufn != NULL ) ldap_memfree( ufn );
898 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
899 a = ldap_next_attribute( ld, entry, ber ) )
902 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
904 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
905 for ( i = 0; bvals[i] != NULL; i++ ) {
906 if ( vals2tmp > 1 || ( vals2tmp
907 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
910 /* write value to file */
911 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
915 if ( mktemp( tmpfname ) == NULL ) {
920 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
925 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
930 if ( fwrite( bvals[ i ]->bv_val,
931 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
940 sprintf( url, "%s%s", urlpre,
941 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
943 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
946 write_ldif( LDIF_PUT_VALUE, a,
947 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
950 ber_bvecfree( bvals );
959 static void print_reference(
961 LDAPMessage *reference )
968 printf("# search reference\n");
971 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
973 if( rc != LDAP_SUCCESS ) {
974 ldap_perror(ld, "ldap_parse_reference");
975 exit( EXIT_FAILURE );
980 for( i=0; refs[i] != NULL; i++ ) {
981 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
982 "ref", refs[i], strlen(refs[i]) );
984 ber_memvfree( (void **) refs );
988 print_ctrls( ctrls );
989 ldap_controls_free( ctrls );
993 static void print_extended(
995 LDAPMessage *extended )
999 struct berval *retdata = NULL;
1002 printf("# extended result response\n");
1005 rc = ldap_parse_extended_result( ld, extended,
1006 &retoid, &retdata, 0 );
1008 if( rc != LDAP_SUCCESS ) {
1009 ldap_perror(ld, "ldap_parse_extended_result");
1010 exit( EXIT_FAILURE );
1013 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1014 "extended", retoid, retoid ? strlen(retoid) : 0 );
1015 ber_memfree( retoid );
1018 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1019 "data", retdata->bv_val, retdata->bv_len );
1020 ber_bvfree( retdata );
1023 print_result( ld, extended, 0 );
1026 static void print_partial(
1028 LDAPMessage *partial )
1031 char *retoid = NULL;
1032 struct berval *retdata = NULL;
1033 LDAPControl **ctrls = NULL;
1036 printf("# extended partial response\n");
1039 rc = ldap_parse_extended_partial( ld, partial,
1040 &retoid, &retdata, &ctrls, 0 );
1042 if( rc != LDAP_SUCCESS ) {
1043 ldap_perror(ld, "ldap_parse_extended_partial");
1044 exit( EXIT_FAILURE );
1047 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1048 "partial", retoid, retoid ? strlen(retoid) : 0 );
1050 ber_memfree( retoid );
1053 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1055 retdata->bv_val, retdata->bv_len );
1057 ber_bvfree( retdata );
1061 print_ctrls( ctrls );
1062 ldap_controls_free( ctrls );
1066 static int print_result(
1068 LDAPMessage *result, int search )
1073 char *matcheddn = NULL;
1076 LDAPControl **ctrls = NULL;
1080 printf("# search result\n");
1083 printf("%s: %d\n", "search", ldap_msgid(result) );
1087 rc = ldap_parse_result( ld, result,
1088 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1090 if( rc != LDAP_SUCCESS ) {
1091 ldap_perror(ld, "ldap_parse_result");
1092 exit( EXIT_FAILURE );
1097 printf( "result: %d %s\n", err, ldap_err2string(err) );
1099 } else if ( err != LDAP_SUCCESS ) {
1100 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1103 if( matcheddn && *matcheddn ) {
1105 write_ldif( LDIF_PUT_VALUE,
1106 "matchedDN", matcheddn, strlen(matcheddn) );
1108 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1111 ber_memfree( matcheddn );
1114 if( text && *text ) {
1116 write_ldif( LDIF_PUT_TEXT, "text",
1117 text, strlen(text) );
1119 fprintf( stderr, "Additional information: %s\n", text );
1122 ber_memfree( text );
1127 for( i=0; refs[i] != NULL; i++ ) {
1129 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1131 fprintf( stderr, "Referral: %s\n", refs[i] );
1135 ber_memvfree( (void **) refs );
1139 print_ctrls( ctrls );
1140 ldap_controls_free( ctrls );
1146 void print_ctrls( LDAPControl **ctrls ) {
1148 for(i=0; ctrls[i] != NULL; i++ ) {
1149 /* control: OID criticality base64value */
1150 struct berval *b64 = NULL;
1154 len = strlen( ctrls[i]->ldctl_oid );
1156 /* add enough for space after OID and the critical value itself */
1157 len += ctrls[i]->ldctl_iscritical
1158 ? sizeof("true") : sizeof("false");
1160 /* convert to base64 */
1161 if( ctrls[i]->ldctl_value.bv_len ) {
1162 b64 = ber_memalloc( sizeof(struct berval) );
1164 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1165 ctrls[i]->ldctl_value.bv_len ) + 1;
1166 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1168 b64->bv_len = lutil_b64_ntop(
1169 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1170 b64->bv_val, b64->bv_len );
1174 len += 1 + b64->bv_len;
1177 str = malloc( len + 1 );
1178 strcpy( str, ctrls[i]->ldctl_oid );
1179 strcat( str, ctrls[i]->ldctl_iscritical
1180 ? " true" : " false" );
1184 strcat(str, b64->bv_val );
1187 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1188 "control", str, len );
1196 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1200 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1204 fputs( ldif, stdout );
1205 ber_memfree( ldif );