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 " -H URI LDAP Uniform Resource Indentifier(s)\n"
74 " -I use SASL Interactive mode\n"
75 " -k use Kerberos authentication\n"
76 " -K like -k, but do only step 1 of the Kerberos bind\n"
77 " -M enable Manage DSA IT control (-MM to make critical)\n"
78 " -n show what would be done but don't actually search\n"
79 " -O props SASL security properties\n"
80 " -p port port on LDAP server\n"
81 " -P version procotol version (default: 3)\n"
82 " -Q use SASL Quiet mode\n"
83 " -R realm SASL realm\n"
84 " -U user SASL authentication identity (username)\n"
85 " -v run in verbose mode (diagnostics to standard output)\n"
86 " -w passwd bind passwd (for simple authentication)\n"
87 " -W prompt for bind passwd\n"
88 " -x Simple authentication\n"
89 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
90 " -Y mech SASL mechanism\n"
91 " -Z Start TLS request (-ZZ to require successful response)\n"
97 static void print_entry LDAP_P((
102 static void print_reference(
104 LDAPMessage *reference );
106 static void print_extended(
108 LDAPMessage *extended );
110 static void print_partial(
112 LDAPMessage *partial );
114 static int print_result(
119 static void print_ctrls(
120 LDAPControl **ctrls );
122 static int write_ldif LDAP_P((
128 static int dosearch LDAP_P((
136 LDAPControl **sctrls,
137 LDAPControl **cctrls,
138 struct timeval *timelimit,
141 static char *tmpdir = NULL;
142 static char *urlpre = NULL;
144 static char *prog = NULL;
145 static char *binddn = NULL;
146 static struct berval passwd = { 0, NULL };
147 static char *base = NULL;
148 static char *ldaphost = NULL;
149 static char *ldapuri = NULL;
150 static int ldapport = 0;
151 #ifdef HAVE_CYRUS_SASL
152 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
153 static char *sasl_realm = NULL;
154 static char *sasl_authc_id = NULL;
155 static char *sasl_authz_id = NULL;
156 static char *sasl_mech = NULL;
157 static char *sasl_secprops = NULL;
159 static int use_tls = 0;
160 static char *sortattr = NULL;
161 static int verbose, not, includeufn, vals2tmp, ldif;
164 main( int argc, char **argv )
166 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
169 int rc, i, first, scope, deref, attrsonly, manageDSAit;
170 int referrals, timelimit, sizelimit, debug;
171 int authmethod, version, want_bindpw;
175 debug = verbose = not = vals2tmp = referrals =
176 attrsonly = manageDSAit = ldif = want_bindpw = 0;
178 lutil_log_initialize(argc, argv);
180 deref = sizelimit = timelimit = version = -1;
182 scope = LDAP_SCOPE_SUBTREE;
185 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
187 while (( i = getopt( argc, argv, "Aa:b:f:Ll:S:s:T:tuV:z:"
188 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z")) != EOF )
192 case 'a': /* set alias deref option */
193 if ( strcasecmp( optarg, "never" ) == 0 ) {
194 deref = LDAP_DEREF_NEVER;
195 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
196 deref = LDAP_DEREF_SEARCHING;
197 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
198 deref = LDAP_DEREF_FINDING;
199 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
200 deref = LDAP_DEREF_ALWAYS;
202 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
206 case 'A': /* retrieve attribute names only -- no values */
209 case 'b': /* search base */
210 base = strdup( optarg );
212 case 'f': /* input file */
213 if( infile != NULL ) {
214 fprintf( stderr, "%s: -f previously specified\n", prog );
217 infile = strdup( optarg );
219 case 'l': /* time limit */
220 timelimit = atoi( optarg );
222 case 'L': /* print entries in LDIF format */
225 case 's': /* search scope */
226 if ( strcasecmp( optarg, "base" ) == 0 ) {
227 scope = LDAP_SCOPE_BASE;
228 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
229 scope = LDAP_SCOPE_ONELEVEL;
230 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
231 scope = LDAP_SCOPE_SUBTREE;
233 fprintf( stderr, "scope should be base, one, or sub\n" );
237 case 'S': /* sort attribute */
238 sortattr = strdup( optarg );
240 case 'u': /* include UFN */
243 case 't': /* write attribute values to TMPDIR files */
246 case 'T': /* tmpdir */
247 if( tmpdir ) free( tmpdir );
248 tmpdir = strdup( optarg );
250 case 'V': /* uri prefix */
251 if( urlpre ) free( urlpre );
252 urlpre = strdup( optarg );
254 case 'z': /* size limit */
255 sizelimit = atoi( optarg );
263 debug |= atoi( optarg );
265 case 'D': /* bind DN */
266 if( binddn != NULL ) {
267 fprintf( stderr, "%s: -D previously specified\n", prog );
270 binddn = strdup( optarg );
272 case 'h': /* ldap host */
273 if( ldapuri != NULL ) {
274 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
277 if( ldaphost != NULL ) {
278 fprintf( stderr, "%s: -h previously specified\n", prog );
281 ldaphost = strdup( optarg );
283 case 'H': /* ldap URI */
284 if( ldaphost != NULL ) {
285 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
289 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
292 if( ldapuri != NULL ) {
293 fprintf( stderr, "%s: -H previously specified\n", prog );
296 ldapuri = strdup( optarg );
299 #ifdef HAVE_CYRUS_SASL
300 if( version == LDAP_VERSION2 ) {
301 fprintf( stderr, "%s: -I incompatible with version %d\n",
305 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
306 fprintf( stderr, "%s: incompatible previous "
307 "authentication choice\n",
311 authmethod = LDAP_AUTH_SASL;
312 version = LDAP_VERSION3;
313 sasl_flags = LDAP_SASL_INTERACTIVE;
316 fprintf( stderr, "%s: was not compiled with SASL support\n",
318 return( EXIT_FAILURE );
320 case 'k': /* kerberos bind */
321 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
322 if( version > LDAP_VERSION2 ) {
323 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
328 if( authmethod != -1 ) {
329 fprintf( stderr, "%s: -k incompatible with previous "
330 "authentication choice\n", prog );
334 authmethod = LDAP_AUTH_KRBV4;
336 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
340 case 'K': /* kerberos bind, part one only */
341 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
342 if( version > LDAP_VERSION2 ) {
343 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
347 if( authmethod != -1 ) {
348 fprintf( stderr, "%s: incompatible with previous "
349 "authentication choice\n", prog );
353 authmethod = LDAP_AUTH_KRBV41;
355 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
356 return( EXIT_FAILURE );
360 /* enable Manage DSA IT */
361 if( version == LDAP_VERSION2 ) {
362 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
367 version = LDAP_VERSION3;
369 case 'n': /* print deletes, don't actually do them */
373 #ifdef HAVE_CYRUS_SASL
374 if( sasl_secprops != NULL ) {
375 fprintf( stderr, "%s: -O previously specified\n", prog );
378 if( version == LDAP_VERSION2 ) {
379 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
383 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
384 fprintf( stderr, "%s: incompatible previous "
385 "authentication choice\n", prog );
388 authmethod = LDAP_AUTH_SASL;
389 version = LDAP_VERSION3;
390 sasl_secprops = strdup( optarg );
392 fprintf( stderr, "%s: not compiled with SASL support\n",
394 return( EXIT_FAILURE );
399 fprintf( stderr, "%s: -p previously specified\n", prog );
402 ldapport = atoi( optarg );
405 switch( atoi(optarg) ) {
407 if( version == LDAP_VERSION3 ) {
408 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
412 version = LDAP_VERSION2;
415 if( version == LDAP_VERSION2 ) {
416 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
420 version = LDAP_VERSION3;
423 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
426 return( EXIT_FAILURE );
429 #ifdef HAVE_CYRUS_SASL
430 if( version == LDAP_VERSION2 ) {
431 fprintf( stderr, "%s: -Q incompatible with version %d\n",
435 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
436 fprintf( stderr, "%s: incompatible previous "
437 "authentication choice\n",
441 authmethod = LDAP_AUTH_SASL;
442 version = LDAP_VERSION3;
443 sasl_flags = LDAP_SASL_QUIET;
446 fprintf( stderr, "%s: not compiled with SASL support\n",
448 return( EXIT_FAILURE );
451 #ifdef HAVE_CYRUS_SASL
452 if( sasl_realm != NULL ) {
453 fprintf( stderr, "%s: -R previously specified\n", prog );
456 if( version == LDAP_VERSION2 ) {
457 fprintf( stderr, "%s: -R incompatible with version %d\n",
461 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
462 fprintf( stderr, "%s: incompatible previous "
463 "authentication choice\n",
467 authmethod = LDAP_AUTH_SASL;
468 version = LDAP_VERSION3;
469 sasl_realm = strdup( optarg );
471 fprintf( stderr, "%s: not compiled with SASL support\n",
473 return( EXIT_FAILURE );
477 #ifdef HAVE_CYRUS_SASL
478 if( sasl_authc_id != NULL ) {
479 fprintf( stderr, "%s: -U previously specified\n", prog );
482 if( version == LDAP_VERSION2 ) {
483 fprintf( stderr, "%s: -U incompatible with version %d\n",
487 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
488 fprintf( stderr, "%s: incompatible previous "
489 "authentication choice\n",
493 authmethod = LDAP_AUTH_SASL;
494 version = LDAP_VERSION3;
495 sasl_authc_id = strdup( optarg );
497 fprintf( stderr, "%s: not compiled with SASL support\n",
499 return( EXIT_FAILURE );
502 case 'v': /* verbose mode */
505 case 'w': /* password */
506 passwd.bv_val = strdup( optarg );
510 for( p = optarg; *p == '\0'; p++ ) {
514 passwd.bv_len = strlen( passwd.bv_val );
520 #ifdef HAVE_CYRUS_SASL
521 if( sasl_mech != NULL ) {
522 fprintf( stderr, "%s: -Y previously specified\n", prog );
525 if( version == LDAP_VERSION2 ) {
526 fprintf( stderr, "%s: -Y incompatible with version %d\n",
530 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
531 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
534 authmethod = LDAP_AUTH_SASL;
535 version = LDAP_VERSION3;
536 sasl_mech = strdup( optarg );
538 fprintf( stderr, "%s: not compiled with SASL support\n",
540 return( EXIT_FAILURE );
544 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
545 fprintf( stderr, "%s: incompatible with previous "
546 "authentication choice\n", prog );
549 authmethod = LDAP_AUTH_SIMPLE;
552 #ifdef HAVE_CYRUS_SASL
553 if( sasl_authz_id != NULL ) {
554 fprintf( stderr, "%s: -X previously specified\n", prog );
557 if( version == LDAP_VERSION2 ) {
558 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
562 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
563 fprintf( stderr, "%s: -X incompatible with "
564 "authentication choice\n", prog );
567 authmethod = LDAP_AUTH_SASL;
568 version = LDAP_VERSION3;
569 sasl_authz_id = strdup( optarg );
571 fprintf( stderr, "%s: not compiled with SASL support\n",
573 return( EXIT_FAILURE );
578 if( version == LDAP_VERSION2 ) {
579 fprintf( stderr, "%s: -Z incompatible with version %d\n",
583 version = LDAP_VERSION3;
586 fprintf( stderr, "%s: not compiled with TLS support\n",
588 return( EXIT_FAILURE );
592 fprintf( stderr, "%s: unrecognized option -%c\n",
599 version = LDAP_VERSION3;
601 if (authmethod == -1 && version > LDAP_VERSION2) {
602 #ifdef HAVE_CYRUS_SASL
603 authmethod = LDAP_AUTH_SASL;
605 authmethod = LDAP_AUTH_SIMPLE;
609 if (( argc - optind < 1 ) ||
610 ( *argv[optind] != '(' /*')'*/ &&
611 ( strchr( argv[optind], '=' ) == NULL ) ) )
613 filtpattern = "(objectclass=*)";
615 filtpattern = strdup( argv[optind++] );
618 if ( (argv[optind] != NULL) && (sortattr == NULL || *sortattr == '\0') ) {
619 attrs = &argv[optind];
622 if ( infile != NULL ) {
623 if ( infile[0] == '-' && infile[1] == '\0' ) {
625 } else if (( fp = fopen( infile, "r" )) == NULL ) {
632 && (tmpdir = getenv("TMPDIR")) == NULL
633 && (tmpdir = getenv("TMP")) == NULL
634 && (tmpdir = getenv("TEMP")) == NULL )
636 tmpdir = LDAP_TMPDIR;
639 if( urlpre == NULL ) {
640 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
642 if( urlpre == NULL ) {
647 sprintf( urlpre, "file:///%s/",
648 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
650 /* urlpre should be URLized.... */
654 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
655 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
657 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
658 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
664 (void) SIGNAL( SIGPIPE, SIG_IGN );
667 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
669 fprintf( stderr, "ldap_init( %s, %d )\n",
670 ldaphost != NULL ? ldaphost : "<DEFAULT>",
674 ld = ldap_init( ldaphost, ldapport );
676 perror("ldapsearch: ldap_init");
682 fprintf( stderr, "ldap_initialize( %s )\n",
683 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
686 rc = ldap_initialize( &ld, ldapuri );
687 if( rc != LDAP_SUCCESS ) {
688 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
689 rc, ldap_err2string(rc) );
695 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
697 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
700 if (timelimit != -1 &&
701 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
703 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
706 if (sizelimit != -1 &&
707 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
709 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
714 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
715 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
717 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
718 referrals ? "on" : "off" );
722 if (version == -1 ) {
723 version = LDAP_VERSION3;
726 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
727 != LDAP_OPT_SUCCESS )
729 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
735 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
737 ldap_perror( ld, "ldap_start_tls" );
740 fprintf( stderr, "WARNING: could not start TLS\n" );
744 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
745 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
748 if ( authmethod == LDAP_AUTH_SASL ) {
749 #ifdef HAVE_CYRUS_SASL
752 if( sasl_secprops != NULL ) {
753 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
754 (void *) sasl_secprops );
756 if( rc != LDAP_OPT_SUCCESS ) {
758 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
760 return( EXIT_FAILURE );
764 defaults = lutil_sasl_defaults( ld,
771 rc = ldap_sasl_interactive_bind_s( ld, binddn,
772 sasl_mech, NULL, NULL,
773 sasl_flags, lutil_sasl_interact, defaults );
775 if( rc != LDAP_SUCCESS ) {
776 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
777 return( EXIT_FAILURE );
780 fprintf( stderr, "%s: not compiled with SASL support\n",
782 return( EXIT_FAILURE );
785 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
787 ldap_perror( ld, "ldap_bind" );
788 return( EXIT_FAILURE );
795 LDAPControl *ctrls[2];
799 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
800 c.ldctl_value.bv_val = NULL;
801 c.ldctl_value.bv_len = 0;
802 c.ldctl_iscritical = manageDSAit > 1;
804 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
806 if( err != LDAP_OPT_SUCCESS ) {
807 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
808 c.ldctl_iscritical ? "critical " : "" );
809 if( c.ldctl_iscritical ) {
810 exit( EXIT_FAILURE );
816 fprintf( stderr, "filter%s: %s\nrequesting: ",
817 infile != NULL ? " pattern" : "",
820 if ( attrs == NULL ) {
821 fprintf( stderr, "ALL" );
823 for ( i = 0; attrs[ i ] != NULL; ++i ) {
824 fprintf( stderr, "%s ", attrs[ i ] );
827 fprintf( stderr, "\n" );
831 printf( "version: %d\n\n", ldif ? 1 : 2 );
835 printf( "#\n# filter%s: %s\n# requesting: ",
836 infile != NULL ? " pattern" : "",
839 if ( attrs == NULL ) {
842 for ( i = 0; attrs[ i ] != NULL; ++i ) {
843 printf( "%s ", attrs[ i ] );
848 printf("\n# with manageDSAit %scontrol",
849 manageDSAit > 1 ? "critical " : "" );
855 if ( infile == NULL ) {
856 rc = dosearch( ld, base, scope, NULL, filtpattern,
857 attrs, attrsonly, NULL, NULL, NULL, -1 );
862 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
863 line[ strlen( line ) - 1 ] = '\0';
869 rc = dosearch( ld, base, scope, filtpattern, line,
870 attrs, attrsonly, NULL, NULL, NULL, -1 );
890 LDAPControl **sctrls,
891 LDAPControl **cctrls,
892 struct timeval *timelimit,
895 char filter[ BUFSIZ ];
902 LDAPMessage *res, *msg;
905 if( filtpatt != NULL ) {
906 sprintf( filter, filtpatt, value );
909 fprintf( stderr, "filter is: (%s)\n", filter );
913 printf( "#\n# filter: %s\n#\n", filter );
917 sprintf( filter, "%s", value );
924 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
925 sctrls, cctrls, timelimit, sizelimit, &msgid );
927 if( rc != LDAP_SUCCESS ) {
928 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
929 prog, ldap_err2string( rc ), rc );
933 nresponses = nentries = nreferences = nextended = npartial = 0;
937 while ((rc = ldap_result( ld, LDAP_RES_ANY,
938 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
942 (void) ldap_sort_entries( ld, &res,
943 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
946 for ( msg = ldap_first_message( ld, res );
948 msg = ldap_next_message( ld, msg ) )
950 if( nresponses++ ) putchar('\n');
952 switch( ldap_msgtype( msg ) ) {
953 case LDAP_RES_SEARCH_ENTRY:
955 print_entry( ld, msg, attrsonly );
958 case LDAP_RES_SEARCH_REFERENCE:
960 print_reference( ld, msg );
963 case LDAP_RES_EXTENDED:
965 print_extended( ld, msg );
967 if( ldap_msgid( msg ) == 0 ) {
968 /* unsolicited extended operation */
973 case LDAP_RES_EXTENDED_PARTIAL:
975 print_partial( ld, msg );
978 case LDAP_RES_SEARCH_RESULT:
979 rc = print_result( ld, msg, 1 );
988 ldap_perror( ld, "ldap_result" );
994 printf( "\n# numResponses: %d\n", nresponses );
995 if( nentries ) printf( "# numEntries: %d\n", nentries );
996 if( nextended ) printf( "# numExtended: %d\n", nextended );
997 if( npartial ) printf( "# numPartial: %d\n", npartial );
998 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1011 char tmpfname[ 256 ];
1014 BerElement *ber = NULL;
1015 struct berval **bvals;
1016 LDAPControl **ctrls = NULL;
1019 dn = ldap_get_dn( ld, entry );
1023 ufn = ldap_dn2ufn( dn );
1024 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1026 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1028 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1030 if( rc != LDAP_SUCCESS ) {
1031 fprintf(stderr, "print_entry: %d\n", rc );
1032 ldap_perror( ld, "ldap_get_entry_controls" );
1033 exit( EXIT_FAILURE );
1037 print_ctrls( ctrls );
1038 ldap_controls_free( ctrls );
1043 ufn = ldap_dn2ufn( dn );
1045 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1048 if( ufn != NULL ) ldap_memfree( ufn );
1051 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1052 a = ldap_next_attribute( ld, entry, ber ) )
1055 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1057 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1058 for ( i = 0; bvals[i] != NULL; i++ ) {
1059 if ( vals2tmp > 1 || ( vals2tmp
1060 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1063 /* write value to file */
1064 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1068 if ( mktemp( tmpfname ) == NULL ) {
1073 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1078 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1083 if ( fwrite( bvals[ i ]->bv_val,
1084 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1093 sprintf( url, "%s%s", urlpre,
1094 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1096 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1099 write_ldif( LDIF_PUT_VALUE, a,
1100 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1103 ber_bvecfree( bvals );
1112 static void print_reference(
1114 LDAPMessage *reference )
1118 LDAPControl **ctrls;
1121 printf("# search reference\n");
1124 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1126 if( rc != LDAP_SUCCESS ) {
1127 ldap_perror(ld, "ldap_parse_reference");
1128 exit( EXIT_FAILURE );
1133 for( i=0; refs[i] != NULL; i++ ) {
1134 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1135 "ref", refs[i], strlen(refs[i]) );
1137 ber_memvfree( (void **) refs );
1141 print_ctrls( ctrls );
1142 ldap_controls_free( ctrls );
1146 static void print_extended(
1148 LDAPMessage *extended )
1151 char *retoid = NULL;
1152 struct berval *retdata = NULL;
1155 printf("# extended result response\n");
1158 rc = ldap_parse_extended_result( ld, extended,
1159 &retoid, &retdata, 0 );
1161 if( rc != LDAP_SUCCESS ) {
1162 ldap_perror(ld, "ldap_parse_extended_result");
1163 exit( EXIT_FAILURE );
1166 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1167 "extended", retoid, retoid ? strlen(retoid) : 0 );
1168 ber_memfree( retoid );
1171 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1172 "data", retdata->bv_val, retdata->bv_len );
1173 ber_bvfree( retdata );
1176 print_result( ld, extended, 0 );
1179 static void print_partial(
1181 LDAPMessage *partial )
1184 char *retoid = NULL;
1185 struct berval *retdata = NULL;
1186 LDAPControl **ctrls = NULL;
1189 printf("# extended partial response\n");
1192 rc = ldap_parse_extended_partial( ld, partial,
1193 &retoid, &retdata, &ctrls, 0 );
1195 if( rc != LDAP_SUCCESS ) {
1196 ldap_perror(ld, "ldap_parse_extended_partial");
1197 exit( EXIT_FAILURE );
1200 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1201 "partial", retoid, retoid ? strlen(retoid) : 0 );
1203 ber_memfree( retoid );
1206 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1208 retdata->bv_val, retdata->bv_len );
1210 ber_bvfree( retdata );
1214 print_ctrls( ctrls );
1215 ldap_controls_free( ctrls );
1219 static int print_result(
1221 LDAPMessage *result, int search )
1225 char *matcheddn = NULL;
1228 LDAPControl **ctrls = NULL;
1232 printf("# search result\n");
1235 printf("%s: %d\n", "search", ldap_msgid(result) );
1239 rc = ldap_parse_result( ld, result,
1240 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1242 if( rc != LDAP_SUCCESS ) {
1243 ldap_perror(ld, "ldap_parse_result");
1244 exit( EXIT_FAILURE );
1249 printf( "result: %d %s\n", err, ldap_err2string(err) );
1251 } else if ( err != LDAP_SUCCESS ) {
1252 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1255 if( matcheddn && *matcheddn ) {
1257 write_ldif( LDIF_PUT_VALUE,
1258 "matchedDN", matcheddn, strlen(matcheddn) );
1260 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1263 ber_memfree( matcheddn );
1266 if( text && *text ) {
1268 write_ldif( LDIF_PUT_TEXT, "text",
1269 text, strlen(text) );
1271 fprintf( stderr, "Additional information: %s\n", text );
1274 ber_memfree( text );
1279 for( i=0; refs[i] != NULL; i++ ) {
1281 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1283 fprintf( stderr, "Referral: %s\n", refs[i] );
1287 ber_memvfree( (void **) refs );
1291 print_ctrls( ctrls );
1292 ldap_controls_free( ctrls );
1298 static void print_ctrls(
1299 LDAPControl **ctrls )
1302 for(i=0; ctrls[i] != NULL; i++ ) {
1303 /* control: OID criticality base64value */
1304 struct berval *b64 = NULL;
1308 len = strlen( ctrls[i]->ldctl_oid );
1310 /* add enough for space after OID and the critical value itself */
1311 len += ctrls[i]->ldctl_iscritical
1312 ? sizeof("true") : sizeof("false");
1314 /* convert to base64 */
1315 if( ctrls[i]->ldctl_value.bv_len ) {
1316 b64 = ber_memalloc( sizeof(struct berval) );
1318 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1319 ctrls[i]->ldctl_value.bv_len ) + 1;
1320 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1322 b64->bv_len = lutil_b64_ntop(
1323 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1324 b64->bv_val, b64->bv_len );
1328 len += 1 + b64->bv_len;
1331 str = malloc( len + 1 );
1332 strcpy( str, ctrls[i]->ldctl_oid );
1333 strcat( str, ctrls[i]->ldctl_iscritical
1334 ? " true" : " false" );
1338 strcat(str, b64->bv_val );
1341 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1342 "control", str, len );
1350 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1354 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1358 fputs( ldif, stdout );
1359 ber_memfree( ldif );