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 " filter\tRFC-2254 compliant LDAP search filter\n"
43 " attributes\twhitespace-separated list of attribute descriptions\n"
44 " which may include:\n"
45 " 1.1 no attributes\n"
46 " * all user attributes\n"
47 " + all operational attributes\n"
50 " -a deref one of never (default), always, search, or find\n"
51 " -A retrieve attribute names only (no values)\n"
52 " -b basedn base dn for search\n"
53 " -l limit time limit (in seconds) for search\n"
54 " -L print responses in LDIFv1 format\n"
55 " -LL print responses in LDIF format without comments\n"
56 " -LLL print responses in LDIF format without comments\n"
58 " -s scope one of base, one, or sub (search scope)\n"
59 " -S attr sort the results by attribute `attr'\n"
60 " -t write binary values to files in temporary directory\n"
61 " -tt write all values to files in temporary directory\n"
62 " -T path write files to directory specified by path (default:\n"
64 " -u include User Friendly entry names in the output\n"
65 " -V prefix URL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
66 " -z limit size limit (in entries) for search\n"
69 " -d level set LDAP debugging level to `level'\n"
70 " -D binddn bind DN\n"
71 " -f file read operations from `file'\n"
72 " -h host LDAP server\n"
73 " -I use SASL Interactive mode\n"
74 " -k use Kerberos authentication\n"
75 " -K like -k, but do only step 1 of the Kerberos bind\n"
76 " -M enable Manage DSA IT control (-MM to make critical)\n"
77 " -n show what would be done but don't actually search\n"
78 " -O props SASL security properties\n"
79 " -p port port on LDAP server\n"
80 " -P version procotol version (default: 3)\n"
81 " -Q use SASL Quiet mode\n"
82 " -R realm SASL realm\n"
83 " -U user SASL authentication identity (username)\n"
84 " -v run in verbose mode (diagnostics to standard output)\n"
85 " -w passwd bind passwd (for simple authentication)\n"
86 " -W prompt for bind passwd\n"
87 " -x Simple authentication\n"
88 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
89 " -Y mech SASL mechanism\n"
90 " -Z Start TLS request (-ZZ to require successful response)\n"
96 static void print_entry LDAP_P((
101 static void print_reference(
103 LDAPMessage *reference );
105 static void print_extended(
107 LDAPMessage *extended );
109 static void print_partial(
111 LDAPMessage *partial );
113 static int print_result(
118 static void print_ctrls(
119 LDAPControl **ctrls );
121 static int write_ldif LDAP_P((
127 static int dosearch LDAP_P((
135 LDAPControl **sctrls,
136 LDAPControl **cctrls,
137 struct timeval *timelimit,
140 static char *tmpdir = NULL;
141 static char *urlpre = NULL;
143 static char *prog = NULL;
144 static char *binddn = NULL;
145 static struct berval passwd = { 0, NULL };
146 static char *base = NULL;
147 static char *ldaphost = NULL;
148 static int ldapport = 0;
149 #ifdef HAVE_CYRUS_SASL
150 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
151 static char *sasl_realm = NULL;
152 static char *sasl_authc_id = NULL;
153 static char *sasl_authz_id = NULL;
154 static char *sasl_mech = NULL;
155 static char *sasl_secprops = NULL;
157 static int use_tls = 0;
158 static char *sortattr = NULL;
159 static int verbose, not, includeufn, vals2tmp, ldif;
162 main( int argc, char **argv )
164 char *infile, *filtpattern, **attrs, line[BUFSIZ];
166 int rc, i, first, scope, deref, attrsonly, manageDSAit;
167 int referrals, timelimit, sizelimit, debug;
168 int authmethod, version, want_bindpw;
172 debug = verbose = not = vals2tmp = referrals =
173 attrsonly = manageDSAit = ldif = want_bindpw = 0;
175 deref = sizelimit = timelimit = version = -1;
177 scope = LDAP_SCOPE_SUBTREE;
180 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
182 while (( i = getopt( argc, argv,
183 "Aa:b:f:Ll:S:s:T:tuV:z:" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z")) != EOF )
187 case 'a': /* set alias deref option */
188 if ( strcasecmp( optarg, "never" ) == 0 ) {
189 deref = LDAP_DEREF_NEVER;
190 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
191 deref = LDAP_DEREF_SEARCHING;
192 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
193 deref = LDAP_DEREF_FINDING;
194 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
195 deref = LDAP_DEREF_ALWAYS;
197 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
201 case 'A': /* retrieve attribute names only -- no values */
204 case 'b': /* search base */
205 base = strdup( optarg );
207 case 'f': /* input file */
208 if( infile != NULL ) {
209 fprintf( stderr, "%s: -f previously specified\n" );
212 infile = strdup( optarg );
214 case 'l': /* time limit */
215 timelimit = atoi( optarg );
217 case 'L': /* print entries in LDIF format */
220 case 's': /* search scope */
221 if ( strcasecmp( optarg, "base" ) == 0 ) {
222 scope = LDAP_SCOPE_BASE;
223 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
224 scope = LDAP_SCOPE_ONELEVEL;
225 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
226 scope = LDAP_SCOPE_SUBTREE;
228 fprintf( stderr, "scope should be base, one, or sub\n" );
232 case 'S': /* sort attribute */
233 sortattr = strdup( optarg );
235 case 'u': /* include UFN */
238 case 't': /* write attribute values to TMPDIR files */
241 case 'T': /* tmpdir */
242 if( tmpdir ) free( tmpdir );
243 tmpdir = strdup( optarg );
245 case 'V': /* uri prefix */
246 if( urlpre ) free( urlpre );
247 urlpre = strdup( optarg );
249 case 'z': /* size limit */
250 sizelimit = atoi( optarg );
258 debug |= atoi( optarg );
260 case 'D': /* bind DN */
261 if( binddn != NULL ) {
262 fprintf( stderr, "%s: -D previously specified\n" );
265 binddn = strdup( optarg );
267 case 'h': /* ldap host */
268 if( ldaphost != NULL ) {
269 fprintf( stderr, "%s: -h previously specified\n" );
272 ldaphost = strdup( optarg );
275 #ifdef HAVE_CYRUS_SASL
276 if( version == LDAP_VERSION2 ) {
277 fprintf( stderr, "%s: -I incompatible with version %d\n",
281 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
282 fprintf( stderr, "%s: incompatible previous "
283 "authentication choice\n",
287 authmethod = LDAP_AUTH_SASL;
288 version = LDAP_VERSION3;
289 sasl_flags = LDAP_SASL_INTERACTIVE;
292 fprintf( stderr, "%s: was not compiled with SASL support\n",
294 return( EXIT_FAILURE );
296 case 'k': /* kerberos bind */
297 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
298 if( version > LDAP_VERSION2 ) {
299 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
304 if( authmethod != -1 ) {
305 fprintf( stderr, "%s: -k incompatible with previous "
306 "authentication choice\n", prog );
310 authmethod = LDAP_AUTH_KRBV4;
312 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
316 case 'K': /* kerberos bind, part one only */
317 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
318 if( version > LDAP_VERSION2 ) {
319 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
323 if( authmethod != -1 ) {
324 fprintf( stderr, "%s: incompatible with previous "
325 "authentication choice\n", prog );
329 authmethod = LDAP_AUTH_KRBV41;
331 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
332 return( EXIT_FAILURE );
336 /* enable Manage DSA IT */
337 if( version == LDAP_VERSION2 ) {
338 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
343 version = LDAP_VERSION3;
345 case 'n': /* print deletes, don't actually do them */
349 #ifdef HAVE_CYRUS_SASL
350 if( sasl_secprops != NULL ) {
351 fprintf( stderr, "%s: -O previously specified\n" );
354 if( version == LDAP_VERSION2 ) {
355 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
359 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
360 fprintf( stderr, "%s: incompatible previous "
361 "authentication choice\n", prog );
364 authmethod = LDAP_AUTH_SASL;
365 version = LDAP_VERSION3;
366 sasl_secprops = strdup( optarg );
368 fprintf( stderr, "%s: not compiled with SASL support\n",
370 return( EXIT_FAILURE );
375 fprintf( stderr, "%s: -p previously specified\n" );
378 ldapport = atoi( optarg );
381 switch( atoi(optarg) ) {
383 if( version == LDAP_VERSION3 ) {
384 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
388 version = LDAP_VERSION2;
391 if( version == LDAP_VERSION2 ) {
392 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
396 version = LDAP_VERSION3;
399 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
402 return( EXIT_FAILURE );
405 #ifdef HAVE_CYRUS_SASL
406 if( version == LDAP_VERSION2 ) {
407 fprintf( stderr, "%s: -Q incompatible with version %d\n",
411 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
412 fprintf( stderr, "%s: incompatible previous "
413 "authentication choice\n",
417 authmethod = LDAP_AUTH_SASL;
418 version = LDAP_VERSION3;
419 sasl_flags = LDAP_SASL_QUIET;
422 fprintf( stderr, "%s: not compiled with SASL support\n",
424 return( EXIT_FAILURE );
427 #ifdef HAVE_CYRUS_SASL
428 if( sasl_realm != NULL ) {
429 fprintf( stderr, "%s: -R previously specified\n" );
432 if( version == LDAP_VERSION2 ) {
433 fprintf( stderr, "%s: -R incompatible with version %d\n",
437 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
438 fprintf( stderr, "%s: incompatible previous "
439 "authentication choice\n",
443 authmethod = LDAP_AUTH_SASL;
444 version = LDAP_VERSION3;
445 sasl_realm = strdup( optarg );
447 fprintf( stderr, "%s: not compiled with SASL support\n",
449 return( EXIT_FAILURE );
453 #ifdef HAVE_CYRUS_SASL
454 if( sasl_authc_id != NULL ) {
455 fprintf( stderr, "%s: -U previously specified\n" );
458 if( version == LDAP_VERSION2 ) {
459 fprintf( stderr, "%s: -U incompatible with version %d\n",
463 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
464 fprintf( stderr, "%s: incompatible previous "
465 "authentication choice\n",
469 authmethod = LDAP_AUTH_SASL;
470 version = LDAP_VERSION3;
471 sasl_authc_id = strdup( optarg );
473 fprintf( stderr, "%s: not compiled with SASL support\n",
475 return( EXIT_FAILURE );
478 case 'v': /* verbose mode */
481 case 'w': /* password */
482 passwd.bv_val = strdup( optarg );
486 for( p = optarg; *p == '\0'; p++ ) {
490 passwd.bv_len = strlen( passwd.bv_val );
496 #ifdef HAVE_CYRUS_SASL
497 if( sasl_mech != NULL ) {
498 fprintf( stderr, "%s: -Y previously specified\n" );
501 if( version == LDAP_VERSION2 ) {
502 fprintf( stderr, "%s: -Y incompatible with version %d\n",
506 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
507 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
510 authmethod = LDAP_AUTH_SASL;
511 version = LDAP_VERSION3;
512 sasl_mech = strdup( optarg );
514 fprintf( stderr, "%s: not compiled with SASL support\n",
516 return( EXIT_FAILURE );
520 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
521 fprintf( stderr, "%s: incompatible with previous "
522 "authentication choice\n", prog );
525 authmethod = LDAP_AUTH_SIMPLE;
528 #ifdef HAVE_CYRUS_SASL
529 if( sasl_authz_id != NULL ) {
530 fprintf( stderr, "%s: -X previously specified\n" );
533 if( version == LDAP_VERSION2 ) {
534 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
538 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
539 fprintf( stderr, "%s: -X incompatible with "
540 "authentication choice\n", prog );
543 authmethod = LDAP_AUTH_SASL;
544 version = LDAP_VERSION3;
545 sasl_authz_id = strdup( optarg );
547 fprintf( stderr, "%s: not compiled with SASL support\n",
549 return( EXIT_FAILURE );
554 if( version == LDAP_VERSION2 ) {
555 fprintf( stderr, "%s: -Z incompatible with version %d\n",
559 version = LDAP_VERSION3;
562 fprintf( stderr, "%s: not compiled with TLS support\n",
564 return( EXIT_FAILURE );
568 fprintf( stderr, "%s: unrecongized option -%c\n",
575 version = LDAP_VERSION3;
577 if (authmethod == -1 && version > LDAP_VERSION2) {
578 #ifdef HAVE_CYRUS_SASL
579 authmethod = LDAP_AUTH_SASL;
581 authmethod = LDAP_AUTH_SIMPLE;
585 if (( argc - optind < 1 ) ||
586 ( strchr( argv[optind], '=' ) == NULL ) )
588 filtpattern = "(objectclass=*)";
590 filtpattern = strdup( argv[optind++] );
593 if ( argv[optind] == NULL ) {
595 } else if ( sortattr == NULL || *sortattr == '\0' ) {
596 attrs = &argv[optind];
599 if ( infile != NULL ) {
600 if ( infile[0] == '-' && infile[1] == '\0' ) {
602 } else if (( fp = fopen( infile, "r" )) == NULL ) {
609 && (tmpdir = getenv("TMPDIR")) == NULL
610 && (tmpdir = getenv("TMP")) == NULL
611 && (tmpdir = getenv("TEMP")) == NULL )
613 tmpdir = LDAP_TMPDIR;
616 if( urlpre == NULL ) {
617 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
619 if( urlpre == NULL ) {
624 sprintf( urlpre, "file:///%s/",
625 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
627 /* urlpre should be URLized.... */
631 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
632 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
634 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
635 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
641 (void) SIGNAL( SIGPIPE, SIG_IGN );
646 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
647 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
651 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
652 perror( "ldap_init" );
657 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
659 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
662 if (timelimit != -1 &&
663 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
665 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
668 if (sizelimit != -1 &&
669 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
671 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
676 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
677 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
679 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
680 referrals ? "on" : "off" );
684 if (version == -1 ) {
688 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
689 != LDAP_OPT_SUCCESS )
691 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
696 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
698 ldap_perror( ld, "ldap_start_tls" );
701 fprintf( stderr, "WARNING: could not start TLS\n" );
705 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
706 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
709 if ( authmethod == LDAP_AUTH_SASL ) {
710 #ifdef HAVE_CYRUS_SASL
713 if( sasl_secprops != NULL ) {
714 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
715 (void *) sasl_secprops );
717 if( rc != LDAP_OPT_SUCCESS ) {
719 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
721 return( EXIT_FAILURE );
725 defaults = lutil_sasl_defaults( ld,
732 rc = ldap_sasl_interactive_bind_s( ld, binddn,
733 sasl_mech, NULL, NULL,
734 sasl_flags, lutil_sasl_interact, defaults );
736 if( rc != LDAP_SUCCESS ) {
737 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
738 return( EXIT_FAILURE );
741 fprintf( stderr, "%s: not compiled with SASL support\n",
743 return( EXIT_FAILURE );
746 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
748 ldap_perror( ld, "ldap_bind" );
749 return( EXIT_FAILURE );
756 LDAPControl *ctrls[2];
760 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
761 c.ldctl_value.bv_val = NULL;
762 c.ldctl_value.bv_len = 0;
763 c.ldctl_iscritical = manageDSAit > 1;
765 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
767 if( err != LDAP_OPT_SUCCESS ) {
768 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
769 c.ldctl_iscritical ? "critical " : "" );
770 if( c.ldctl_iscritical ) {
771 exit( EXIT_FAILURE );
777 fprintf( stderr, "filter%s: %s\nrequesting: ",
778 infile != NULL ? " pattern" : "",
781 if ( attrs == NULL ) {
782 fprintf( stderr, "ALL" );
784 for ( i = 0; attrs[ i ] != NULL; ++i ) {
785 fprintf( stderr, "%s ", attrs[ i ] );
788 fprintf( stderr, "\n" );
792 printf( "version: %d\n\n", ldif ? 1 : 2 );
796 printf( "#\n# filter%s: %s\n# requesting: ",
797 infile != NULL ? " pattern" : "",
800 if ( attrs == NULL ) {
803 for ( i = 0; attrs[ i ] != NULL; ++i ) {
804 printf( "%s ", attrs[ i ] );
809 printf("\n# with manageDSAit %scontrol",
810 manageDSAit > 1 ? "critical " : "" );
816 if ( infile == NULL ) {
817 rc = dosearch( ld, base, scope, NULL, filtpattern,
818 attrs, attrsonly, NULL, NULL, NULL, -1 );
823 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
824 line[ strlen( line ) - 1 ] = '\0';
830 rc = dosearch( ld, base, scope, filtpattern, line,
831 attrs, attrsonly, NULL, NULL, NULL, -1 );
851 LDAPControl **sctrls,
852 LDAPControl **cctrls,
853 struct timeval *timelimit,
856 char filter[ BUFSIZ ];
863 LDAPMessage *res, *msg;
866 if( filtpatt != NULL ) {
867 sprintf( filter, filtpatt, value );
870 fprintf( stderr, "filter is: (%s)\n", filter );
874 printf( "#\n# filter: %s\n#\n", filter );
878 sprintf( filter, "%s", value );
885 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
886 sctrls, cctrls, timelimit, sizelimit, &msgid );
888 if( rc != LDAP_SUCCESS ) {
889 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
890 prog, ldap_err2string( rc ), rc );
894 nresponses = nentries = nreferences = nextended = npartial = 0;
898 while ((rc = ldap_result( ld, LDAP_RES_ANY,
899 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
903 (void) ldap_sort_entries( ld, &res,
904 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
907 for ( msg = ldap_first_message( ld, res );
909 msg = ldap_next_message( ld, msg ) )
911 if( nresponses++ ) putchar('\n');
913 switch( ldap_msgtype( msg ) ) {
914 case LDAP_RES_SEARCH_ENTRY:
916 print_entry( ld, msg, attrsonly );
919 case LDAP_RES_SEARCH_REFERENCE:
921 print_reference( ld, msg );
924 case LDAP_RES_EXTENDED:
926 print_extended( ld, msg );
928 if( ldap_msgid( msg ) == 0 ) {
929 /* unsolicited extended operation */
934 case LDAP_RES_EXTENDED_PARTIAL:
936 print_partial( ld, msg );
939 case LDAP_RES_SEARCH_RESULT:
940 rc = print_result( ld, msg, 1 );
949 ldap_perror( ld, "ldap_result" );
955 printf( "\n# numResponses: %d\n", nresponses );
956 if( nentries ) printf( "# numEntries: %d\n", nentries );
957 if( nextended ) printf( "# numExtended: %d\n", nextended );
958 if( npartial ) printf( "# numPartial: %d\n", npartial );
959 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
972 char tmpfname[ 256 ];
975 BerElement *ber = NULL;
976 struct berval **bvals;
977 LDAPControl **ctrls = NULL;
980 dn = ldap_get_dn( ld, entry );
984 ufn = ldap_dn2ufn( dn );
985 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
987 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
989 rc = ldap_get_entry_controls( ld, entry, &ctrls );
991 if( rc != LDAP_SUCCESS ) {
992 fprintf(stderr, "print_entry: %d\n", rc );
993 ldap_perror( ld, "ldap_get_entry_controls" );
994 exit( EXIT_FAILURE );
998 print_ctrls( ctrls );
999 ldap_controls_free( ctrls );
1004 ufn = ldap_dn2ufn( dn );
1006 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1009 if( ufn != NULL ) ldap_memfree( ufn );
1012 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1013 a = ldap_next_attribute( ld, entry, ber ) )
1016 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1018 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1019 for ( i = 0; bvals[i] != NULL; i++ ) {
1020 if ( vals2tmp > 1 || ( vals2tmp
1021 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1024 /* write value to file */
1025 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1029 if ( mktemp( tmpfname ) == NULL ) {
1034 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1039 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1044 if ( fwrite( bvals[ i ]->bv_val,
1045 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1054 sprintf( url, "%s%s", urlpre,
1055 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1057 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1060 write_ldif( LDIF_PUT_VALUE, a,
1061 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1064 ber_bvecfree( bvals );
1073 static void print_reference(
1075 LDAPMessage *reference )
1079 LDAPControl **ctrls;
1082 printf("# search reference\n");
1085 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1087 if( rc != LDAP_SUCCESS ) {
1088 ldap_perror(ld, "ldap_parse_reference");
1089 exit( EXIT_FAILURE );
1094 for( i=0; refs[i] != NULL; i++ ) {
1095 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1096 "ref", refs[i], strlen(refs[i]) );
1098 ber_memvfree( (void **) refs );
1102 print_ctrls( ctrls );
1103 ldap_controls_free( ctrls );
1107 static void print_extended(
1109 LDAPMessage *extended )
1112 char *retoid = NULL;
1113 struct berval *retdata = NULL;
1116 printf("# extended result response\n");
1119 rc = ldap_parse_extended_result( ld, extended,
1120 &retoid, &retdata, 0 );
1122 if( rc != LDAP_SUCCESS ) {
1123 ldap_perror(ld, "ldap_parse_extended_result");
1124 exit( EXIT_FAILURE );
1127 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1128 "extended", retoid, retoid ? strlen(retoid) : 0 );
1129 ber_memfree( retoid );
1132 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1133 "data", retdata->bv_val, retdata->bv_len );
1134 ber_bvfree( retdata );
1137 print_result( ld, extended, 0 );
1140 static void print_partial(
1142 LDAPMessage *partial )
1145 char *retoid = NULL;
1146 struct berval *retdata = NULL;
1147 LDAPControl **ctrls = NULL;
1150 printf("# extended partial response\n");
1153 rc = ldap_parse_extended_partial( ld, partial,
1154 &retoid, &retdata, &ctrls, 0 );
1156 if( rc != LDAP_SUCCESS ) {
1157 ldap_perror(ld, "ldap_parse_extended_partial");
1158 exit( EXIT_FAILURE );
1161 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1162 "partial", retoid, retoid ? strlen(retoid) : 0 );
1164 ber_memfree( retoid );
1167 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1169 retdata->bv_val, retdata->bv_len );
1171 ber_bvfree( retdata );
1175 print_ctrls( ctrls );
1176 ldap_controls_free( ctrls );
1180 static int print_result(
1182 LDAPMessage *result, int search )
1187 char *matcheddn = NULL;
1190 LDAPControl **ctrls = NULL;
1194 printf("# search result\n");
1197 printf("%s: %d\n", "search", ldap_msgid(result) );
1201 rc = ldap_parse_result( ld, result,
1202 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1204 if( rc != LDAP_SUCCESS ) {
1205 ldap_perror(ld, "ldap_parse_result");
1206 exit( EXIT_FAILURE );
1211 printf( "result: %d %s\n", err, ldap_err2string(err) );
1213 } else if ( err != LDAP_SUCCESS ) {
1214 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1217 if( matcheddn && *matcheddn ) {
1219 write_ldif( LDIF_PUT_VALUE,
1220 "matchedDN", matcheddn, strlen(matcheddn) );
1222 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1225 ber_memfree( matcheddn );
1228 if( text && *text ) {
1230 write_ldif( LDIF_PUT_TEXT, "text",
1231 text, strlen(text) );
1233 fprintf( stderr, "Additional information: %s\n", text );
1236 ber_memfree( text );
1241 for( i=0; refs[i] != NULL; i++ ) {
1243 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1245 fprintf( stderr, "Referral: %s\n", refs[i] );
1249 ber_memvfree( (void **) refs );
1253 print_ctrls( ctrls );
1254 ldap_controls_free( ctrls );
1260 void print_ctrls( LDAPControl **ctrls ) {
1262 for(i=0; ctrls[i] != NULL; i++ ) {
1263 /* control: OID criticality base64value */
1264 struct berval *b64 = NULL;
1268 len = strlen( ctrls[i]->ldctl_oid );
1270 /* add enough for space after OID and the critical value itself */
1271 len += ctrls[i]->ldctl_iscritical
1272 ? sizeof("true") : sizeof("false");
1274 /* convert to base64 */
1275 if( ctrls[i]->ldctl_value.bv_len ) {
1276 b64 = ber_memalloc( sizeof(struct berval) );
1278 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1279 ctrls[i]->ldctl_value.bv_len ) + 1;
1280 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1282 b64->bv_len = lutil_b64_ntop(
1283 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1284 b64->bv_val, b64->bv_len );
1288 len += 1 + b64->bv_len;
1291 str = malloc( len + 1 );
1292 strcpy( str, ctrls[i]->ldctl_oid );
1293 strcat( str, ctrls[i]->ldctl_iscritical
1294 ? " true" : " false" );
1298 strcat(str, b64->bv_val );
1301 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1302 "control", str, len );
1310 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1314 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1318 fputs( ldif, stdout );
1319 ber_memfree( ldif );