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 #ifdef HAVE_CYRUS_SASL
468 authmethod = LDAP_AUTH_SASL;
470 authmethod = LDAP_AUTH_SIMPLE;
474 if (( argc - optind < 1 ) ||
475 ( strchr( argv[optind], '=' ) == NULL ) )
477 filtpattern = "(objectclass=*)";
479 filtpattern = strdup( argv[optind++] );
482 if ( argv[optind] == NULL ) {
484 } else if ( sortattr == NULL || *sortattr == '\0' ) {
485 attrs = &argv[optind];
488 if ( infile != NULL ) {
489 if ( infile[0] == '-' && infile[1] == '\0' ) {
491 } else if (( fp = fopen( infile, "r" )) == NULL ) {
498 && (tmpdir = getenv("TMPDIR")) == NULL
499 && (tmpdir = getenv("TMP")) == NULL
500 && (tmpdir = getenv("TEMP")) == NULL )
502 tmpdir = LDAP_TMPDIR;
505 if( urlpre == NULL ) {
506 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
508 if( urlpre == NULL ) {
513 sprintf( urlpre, "file:///%s/",
514 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
516 /* urlpre should be URLized.... */
520 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
521 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
523 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
524 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
530 (void) SIGNAL( SIGPIPE, SIG_IGN );
535 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
536 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
540 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
541 perror( "ldap_init" );
546 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
548 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
551 if (timelimit != -1 &&
552 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
554 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
557 if (sizelimit != -1 &&
558 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
560 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
565 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
566 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
568 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
569 referrals ? "on" : "off" );
573 if (version == -1 ) {
577 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
578 != LDAP_OPT_SUCCESS )
580 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
585 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
587 ldap_perror( ld, "ldap_start_tls" );
590 fprintf( stderr, "WARNING: could not start TLS\n" );
594 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
595 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
598 if ( authmethod == LDAP_AUTH_SASL ) {
599 #ifdef HAVE_CYRUS_SASL
600 if( sasl_secprops != NULL ) {
601 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
602 (void *) sasl_secprops );
604 if( rc != LDAP_OPT_SUCCESS ) {
606 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
608 return( EXIT_FAILURE );
612 rc = ldap_sasl_interactive_bind_s( ld, binddn,
613 sasl_mech, NULL, NULL, lutil_sasl_interact );
615 if( rc != LDAP_SUCCESS ) {
616 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
617 return( EXIT_FAILURE );
620 fprintf( stderr, "%s was not compiled with SASL support\n",
622 return( EXIT_FAILURE );
625 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
627 ldap_perror( ld, "ldap_bind" );
628 return( EXIT_FAILURE );
635 LDAPControl *ctrls[2];
639 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
640 c.ldctl_value.bv_val = NULL;
641 c.ldctl_value.bv_len = 0;
642 c.ldctl_iscritical = manageDSAit > 1;
644 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
646 if( err != LDAP_OPT_SUCCESS ) {
647 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
648 c.ldctl_iscritical ? "critical " : "" );
649 if( c.ldctl_iscritical ) {
650 exit( EXIT_FAILURE );
656 fprintf( stderr, "filter%s: %s\nrequesting: ",
657 infile != NULL ? " pattern" : "",
660 if ( attrs == NULL ) {
661 fprintf( stderr, "ALL" );
663 for ( i = 0; attrs[ i ] != NULL; ++i ) {
664 fprintf( stderr, "%s ", attrs[ i ] );
667 fprintf( stderr, "\n" );
671 printf( "version: %d\n\n", ldif ? 1 : 2 );
675 printf( "#\n# filter%s: %s\n# requesting: ",
676 infile != NULL ? " pattern" : "",
679 if ( attrs == NULL ) {
682 for ( i = 0; attrs[ i ] != NULL; ++i ) {
683 printf( "%s ", attrs[ i ] );
688 printf("\n# with manageDSAit %scontrol",
689 manageDSAit > 1 ? "critical " : "" );
695 if ( infile == NULL ) {
696 rc = dosearch( ld, base, scope, NULL, filtpattern,
697 attrs, attrsonly, NULL, NULL, NULL, -1 );
702 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
703 line[ strlen( line ) - 1 ] = '\0';
709 rc = dosearch( ld, base, scope, filtpattern, line,
710 attrs, attrsonly, NULL, NULL, NULL, -1 );
730 LDAPControl **sctrls,
731 LDAPControl **cctrls,
732 struct timeval *timelimit,
735 char filter[ BUFSIZ ];
742 LDAPMessage *res, *msg;
745 if( filtpatt != NULL ) {
746 sprintf( filter, filtpatt, value );
749 fprintf( stderr, "filter is: (%s)\n", filter );
753 printf( "#\n# filter: %s\n#\n", filter );
757 sprintf( filter, "%s", value );
764 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
765 sctrls, cctrls, timelimit, sizelimit, &msgid );
767 if( rc != LDAP_SUCCESS ) {
768 fprintf( stderr, "ldapsearch: ldap_search_ext: %s (%d)\n",
769 ldap_err2string( rc ), rc );
773 nresponses = nentries = nreferences = nextended = npartial = 0;
777 while ((rc = ldap_result( ld, LDAP_RES_ANY,
778 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
782 (void) ldap_sort_entries( ld, &res,
783 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
786 for ( msg = ldap_first_message( ld, res );
788 msg = ldap_next_message( ld, msg ) )
790 if( nresponses++ ) putchar('\n');
792 switch( ldap_msgtype( msg ) ) {
793 case LDAP_RES_SEARCH_ENTRY:
795 print_entry( ld, msg, attrsonly );
798 case LDAP_RES_SEARCH_REFERENCE:
800 print_reference( ld, msg );
803 case LDAP_RES_EXTENDED:
805 print_extended( ld, msg );
807 if( ldap_msgid( msg ) == 0 ) {
808 /* unsolicited extended operation */
813 case LDAP_RES_EXTENDED_PARTIAL:
815 print_partial( ld, msg );
818 case LDAP_RES_SEARCH_RESULT:
819 rc = print_result( ld, msg, 1 );
828 ldap_perror( ld, "ldap_result" );
834 printf( "\n# numResponses: %d\n", nresponses );
835 if( nentries ) printf( "# numEntries: %d\n", nentries );
836 if( nextended ) printf( "# numExtended: %d\n", nextended );
837 if( npartial ) printf( "# numPartial: %d\n", npartial );
838 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
851 char tmpfname[ 256 ];
854 BerElement *ber = NULL;
855 struct berval **bvals;
856 LDAPControl **ctrls = NULL;
859 dn = ldap_get_dn( ld, entry );
863 ufn = ldap_dn2ufn( dn );
864 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
866 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
868 rc = ldap_get_entry_controls( ld, entry, &ctrls );
870 if( rc != LDAP_SUCCESS ) {
871 fprintf(stderr, "print_entry: %d\n", rc );
872 ldap_perror( ld, "ldap_get_entry_controls" );
873 exit( EXIT_FAILURE );
877 print_ctrls( ctrls );
878 ldap_controls_free( ctrls );
883 ufn = ldap_dn2ufn( dn );
885 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
888 if( ufn != NULL ) ldap_memfree( ufn );
891 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
892 a = ldap_next_attribute( ld, entry, ber ) )
895 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
897 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
898 for ( i = 0; bvals[i] != NULL; i++ ) {
899 if ( vals2tmp > 1 || ( vals2tmp
900 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
903 /* write value to file */
904 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
908 if ( mktemp( tmpfname ) == NULL ) {
913 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
918 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
923 if ( fwrite( bvals[ i ]->bv_val,
924 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
933 sprintf( url, "%s%s", urlpre,
934 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
936 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
939 write_ldif( LDIF_PUT_VALUE, a,
940 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
943 ber_bvecfree( bvals );
952 static void print_reference(
954 LDAPMessage *reference )
961 printf("# search reference\n");
964 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
966 if( rc != LDAP_SUCCESS ) {
967 ldap_perror(ld, "ldap_parse_reference");
968 exit( EXIT_FAILURE );
973 for( i=0; refs[i] != NULL; i++ ) {
974 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
975 "ref", refs[i], strlen(refs[i]) );
977 ber_memvfree( (void **) refs );
981 print_ctrls( ctrls );
982 ldap_controls_free( ctrls );
986 static void print_extended(
988 LDAPMessage *extended )
992 struct berval *retdata = NULL;
995 printf("# extended result response\n");
998 rc = ldap_parse_extended_result( ld, extended,
999 &retoid, &retdata, 0 );
1001 if( rc != LDAP_SUCCESS ) {
1002 ldap_perror(ld, "ldap_parse_extended_result");
1003 exit( EXIT_FAILURE );
1006 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1007 "extended", retoid, retoid ? strlen(retoid) : 0 );
1008 ber_memfree( retoid );
1011 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1012 "data", retdata->bv_val, retdata->bv_len );
1013 ber_bvfree( retdata );
1016 print_result( ld, extended, 0 );
1019 static void print_partial(
1021 LDAPMessage *partial )
1024 char *retoid = NULL;
1025 struct berval *retdata = NULL;
1026 LDAPControl **ctrls = NULL;
1029 printf("# extended partial response\n");
1032 rc = ldap_parse_extended_partial( ld, partial,
1033 &retoid, &retdata, &ctrls, 0 );
1035 if( rc != LDAP_SUCCESS ) {
1036 ldap_perror(ld, "ldap_parse_extended_partial");
1037 exit( EXIT_FAILURE );
1040 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1041 "partial", retoid, retoid ? strlen(retoid) : 0 );
1043 ber_memfree( retoid );
1046 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1048 retdata->bv_val, retdata->bv_len );
1050 ber_bvfree( retdata );
1054 print_ctrls( ctrls );
1055 ldap_controls_free( ctrls );
1059 static int print_result(
1061 LDAPMessage *result, int search )
1066 char *matcheddn = NULL;
1069 LDAPControl **ctrls = NULL;
1073 printf("# search result\n");
1076 printf("%s: %d\n", "search", ldap_msgid(result) );
1080 rc = ldap_parse_result( ld, result,
1081 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1083 if( rc != LDAP_SUCCESS ) {
1084 ldap_perror(ld, "ldap_parse_result");
1085 exit( EXIT_FAILURE );
1090 printf( "result: %d %s\n", err, ldap_err2string(err) );
1092 } else if ( err != LDAP_SUCCESS ) {
1093 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1096 if( matcheddn && *matcheddn ) {
1098 write_ldif( LDIF_PUT_VALUE,
1099 "matchedDN", matcheddn, strlen(matcheddn) );
1101 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1104 ber_memfree( matcheddn );
1107 if( text && *text ) {
1109 write_ldif( LDIF_PUT_TEXT, "text",
1110 text, strlen(text) );
1112 fprintf( stderr, "Additional information: %s\n", text );
1115 ber_memfree( text );
1120 for( i=0; refs[i] != NULL; i++ ) {
1122 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1124 fprintf( stderr, "Referral: %s\n", refs[i] );
1128 ber_memvfree( (void **) refs );
1132 print_ctrls( ctrls );
1133 ldap_controls_free( ctrls );
1139 void print_ctrls( LDAPControl **ctrls ) {
1141 for(i=0; ctrls[i] != NULL; i++ ) {
1142 /* control: OID criticality base64value */
1143 struct berval *b64 = NULL;
1147 len = strlen( ctrls[i]->ldctl_oid );
1149 /* add enough for space after OID and the critical value itself */
1150 len += ctrls[i]->ldctl_iscritical
1151 ? sizeof("true") : sizeof("false");
1153 /* convert to base64 */
1154 if( ctrls[i]->ldctl_value.bv_len ) {
1155 b64 = ber_memalloc( sizeof(struct berval) );
1157 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1158 ctrls[i]->ldctl_value.bv_len ) + 1;
1159 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1161 b64->bv_len = lutil_b64_ntop(
1162 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1163 b64->bv_val, b64->bv_len );
1167 len += 1 + b64->bv_len;
1170 str = malloc( len + 1 );
1171 strcpy( str, ctrls[i]->ldctl_oid );
1172 strcat( str, ctrls[i]->ldctl_iscritical
1173 ? " true" : " false" );
1177 strcat(str, b64->bv_val );
1180 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1181 "control", str, len );
1189 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1193 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1197 fputs( ldif, stdout );
1198 ber_memfree( ldif );