3 * Copyright 1998-2002 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 static char *def_tmpdir;
39 static char *def_urlpre;
42 usage( const char *s )
45 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
46 " filter\tRFC-2254 compliant LDAP search filter\n"
47 " attributes\twhitespace-separated list of attribute descriptions\n"
48 " which may include:\n"
49 " 1.1 no attributes\n"
50 " * all user attributes\n"
51 " + all operational attributes\n"
54 " -a deref one of never (default), always, search, or find\n"
55 " -A retrieve attribute names only (no values)\n"
56 " -b basedn base dn for search\n"
57 " -F prefix URL prefix for files (default: %s)\n"
58 " -l limit time limit (in seconds) for search\n"
59 " -L print responses in LDIFv1 format\n"
60 " -LL print responses in LDIF format without comments\n"
61 " -LLL print responses in LDIF format without comments\n"
63 " -s scope one of base, one, or sub (search scope)\n"
64 " -S attr sort the results by attribute `attr'\n"
65 " -t write binary values to files in temporary directory\n"
66 " -tt write all values to files in temporary directory\n"
67 " -T path write files to directory specified by path (default: %s)\n"
68 " -u include User Friendly entry names in the output\n"
69 " -z limit size limit (in entries) for search\n"
72 " -d level set LDAP debugging level to `level'\n"
73 " -D binddn bind DN\n"
74 " -f file read operations from `file'\n"
75 " -h host LDAP server\n"
76 " -H URI LDAP Uniform Resource Indentifier(s)\n"
77 " -I use SASL Interactive mode\n"
78 " -k use Kerberos authentication\n"
79 " -K like -k, but do only step 1 of the Kerberos bind\n"
80 " -M enable Manage DSA IT control (-MM to make critical)\n"
81 " -n show what would be done but don't actually search\n"
82 " -O props SASL security properties\n"
83 " -p port port on LDAP server\n"
84 " -P version procotol version (default: 3)\n"
85 " -Q use SASL Quiet mode\n"
86 " -R realm SASL realm\n"
87 " -U authcid SASL authentication identity\n"
88 " -v run in verbose mode (diagnostics to standard output)\n"
89 " -w passwd bind passwd (for simple authentication)\n"
90 " -W prompt for bind passwd\n"
91 " -x Simple authentication\n"
92 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
93 " -Y mech SASL mechanism\n"
94 " -Z Start TLS request (-ZZ to require successful response)\n"
95 , s, def_urlpre, def_tmpdir );
100 static void print_entry LDAP_P((
105 static void print_reference(
107 LDAPMessage *reference );
109 static void print_extended(
111 LDAPMessage *extended );
113 static void print_partial(
115 LDAPMessage *partial );
117 static int print_result(
122 static void print_ctrls(
123 LDAPControl **ctrls );
125 static int write_ldif LDAP_P((
131 static int dosearch LDAP_P((
139 LDAPControl **sctrls,
140 LDAPControl **cctrls,
141 struct timeval *timeout,
144 static char *tmpdir = NULL;
145 static char *urlpre = NULL;
146 static char *prog = NULL;
147 static char *binddn = NULL;
148 static struct berval passwd = { 0, NULL };
149 static char *base = NULL;
150 static char *ldaphost = NULL;
151 static char *ldapuri = NULL;
152 static int ldapport = 0;
153 #ifdef HAVE_CYRUS_SASL
154 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
155 static char *sasl_realm = NULL;
156 static char *sasl_authc_id = NULL;
157 static char *sasl_authz_id = NULL;
158 static char *sasl_mech = NULL;
159 static char *sasl_secprops = NULL;
161 static int use_tls = 0;
162 static char *sortattr = NULL;
163 static int verbose, not, includeufn, vals2tmp, ldif;
170 if (*LDAP_DIRSEP != '/')
172 for (p = url; *p; p++)
174 if (*p == *LDAP_DIRSEP)
181 main( int argc, char **argv )
183 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
185 int rc, i, first, scope, deref, attrsonly, manageDSAit;
186 int referrals, timelimit, sizelimit, debug;
187 int authmethod, version, want_bindpw;
191 debug = verbose = not = vals2tmp = referrals =
192 attrsonly = manageDSAit = ldif = want_bindpw = 0;
194 lutil_log_initialize(argc, argv);
196 deref = sizelimit = timelimit = version = -1;
198 scope = LDAP_SCOPE_SUBTREE;
201 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
202 (def_tmpdir = getenv("TMP")) == NULL &&
203 (def_tmpdir = getenv("TEMP")) == NULL )
205 def_tmpdir = LDAP_TMPDIR;
209 def_tmpdir = LDAP_TMPDIR;
211 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
213 if( def_urlpre == NULL ) {
218 sprintf( def_urlpre, "file:///%s/",
219 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
221 urlize( def_urlpre );
223 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
225 while (( i = getopt( argc, argv, "Aa:b:F:f:Ll:S:s:T:tuz:"
226 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z")) != EOF )
230 case 'a': /* set alias deref option */
231 if ( strcasecmp( optarg, "never" ) == 0 ) {
232 deref = LDAP_DEREF_NEVER;
233 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
234 deref = LDAP_DEREF_SEARCHING;
235 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
236 deref = LDAP_DEREF_FINDING;
237 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
238 deref = LDAP_DEREF_ALWAYS;
240 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
244 case 'A': /* retrieve attribute names only -- no values */
247 case 'b': /* search base */
248 base = strdup( optarg );
250 case 'f': /* input file */
251 if( infile != NULL ) {
252 fprintf( stderr, "%s: -f previously specified\n", prog );
255 infile = strdup( optarg );
257 case 'F': /* uri prefix */
258 if( urlpre ) free( urlpre );
259 urlpre = strdup( optarg );
261 case 'l': /* time limit */
262 timelimit = atoi( optarg );
263 if( timelimit < 0 ) {
264 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
269 case 'L': /* print entries in LDIF format */
272 case 's': /* search scope */
273 if ( strcasecmp( optarg, "base" ) == 0 ) {
274 scope = LDAP_SCOPE_BASE;
275 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
276 scope = LDAP_SCOPE_ONELEVEL;
277 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
278 scope = LDAP_SCOPE_SUBTREE;
280 fprintf( stderr, "scope should be base, one, or sub\n" );
284 case 'S': /* sort attribute */
285 sortattr = strdup( optarg );
287 case 'u': /* include UFN */
290 case 't': /* write attribute values to TMPDIR files */
293 case 'T': /* tmpdir */
294 if( tmpdir ) free( tmpdir );
295 tmpdir = strdup( optarg );
297 case 'z': /* size limit */
298 sizelimit = atoi( optarg );
306 debug |= atoi( optarg );
308 case 'D': /* bind DN */
309 if( binddn != NULL ) {
310 fprintf( stderr, "%s: -D previously specified\n", prog );
313 binddn = strdup( optarg );
315 case 'h': /* ldap host */
316 if( ldapuri != NULL ) {
317 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
320 if( ldaphost != NULL ) {
321 fprintf( stderr, "%s: -h previously specified\n", prog );
324 ldaphost = strdup( optarg );
326 case 'H': /* ldap URI */
327 if( ldaphost != NULL ) {
328 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
332 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
335 if( ldapuri != NULL ) {
336 fprintf( stderr, "%s: -H previously specified\n", prog );
339 ldapuri = strdup( optarg );
342 #ifdef HAVE_CYRUS_SASL
343 if( version == LDAP_VERSION2 ) {
344 fprintf( stderr, "%s: -I incompatible with version %d\n",
348 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
349 fprintf( stderr, "%s: incompatible previous "
350 "authentication choice\n",
354 authmethod = LDAP_AUTH_SASL;
355 version = LDAP_VERSION3;
356 sasl_flags = LDAP_SASL_INTERACTIVE;
359 fprintf( stderr, "%s: was not compiled with SASL support\n",
361 return( EXIT_FAILURE );
363 case 'k': /* kerberos bind */
364 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
365 if( version > LDAP_VERSION2 ) {
366 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
371 if( authmethod != -1 ) {
372 fprintf( stderr, "%s: -k incompatible with previous "
373 "authentication choice\n", prog );
377 authmethod = LDAP_AUTH_KRBV4;
379 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
383 case 'K': /* kerberos bind, part one only */
384 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
385 if( version > LDAP_VERSION2 ) {
386 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
390 if( authmethod != -1 ) {
391 fprintf( stderr, "%s: incompatible with previous "
392 "authentication choice\n", prog );
396 authmethod = LDAP_AUTH_KRBV41;
398 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
399 return( EXIT_FAILURE );
403 /* enable Manage DSA IT */
404 if( version == LDAP_VERSION2 ) {
405 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
410 version = LDAP_VERSION3;
412 case 'n': /* print deletes, don't actually do them */
416 #ifdef HAVE_CYRUS_SASL
417 if( sasl_secprops != NULL ) {
418 fprintf( stderr, "%s: -O previously specified\n", prog );
421 if( version == LDAP_VERSION2 ) {
422 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
426 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
427 fprintf( stderr, "%s: incompatible previous "
428 "authentication choice\n", prog );
431 authmethod = LDAP_AUTH_SASL;
432 version = LDAP_VERSION3;
433 sasl_secprops = strdup( optarg );
435 fprintf( stderr, "%s: not compiled with SASL support\n",
437 return( EXIT_FAILURE );
442 fprintf( stderr, "%s: -p previously specified\n", prog );
445 ldapport = atoi( optarg );
448 switch( atoi(optarg) ) {
450 if( version == LDAP_VERSION3 ) {
451 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
455 version = LDAP_VERSION2;
458 if( version == LDAP_VERSION2 ) {
459 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
463 version = LDAP_VERSION3;
466 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
469 return( EXIT_FAILURE );
472 #ifdef HAVE_CYRUS_SASL
473 if( version == LDAP_VERSION2 ) {
474 fprintf( stderr, "%s: -Q incompatible with version %d\n",
478 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
479 fprintf( stderr, "%s: incompatible previous "
480 "authentication choice\n",
484 authmethod = LDAP_AUTH_SASL;
485 version = LDAP_VERSION3;
486 sasl_flags = LDAP_SASL_QUIET;
489 fprintf( stderr, "%s: not compiled with SASL support\n",
491 return( EXIT_FAILURE );
494 #ifdef HAVE_CYRUS_SASL
495 if( sasl_realm != NULL ) {
496 fprintf( stderr, "%s: -R previously specified\n", prog );
499 if( version == LDAP_VERSION2 ) {
500 fprintf( stderr, "%s: -R incompatible with version %d\n",
504 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
505 fprintf( stderr, "%s: incompatible previous "
506 "authentication choice\n",
510 authmethod = LDAP_AUTH_SASL;
511 version = LDAP_VERSION3;
512 sasl_realm = strdup( optarg );
514 fprintf( stderr, "%s: not compiled with SASL support\n",
516 return( EXIT_FAILURE );
520 #ifdef HAVE_CYRUS_SASL
521 if( sasl_authc_id != NULL ) {
522 fprintf( stderr, "%s: -U previously specified\n", prog );
525 if( version == LDAP_VERSION2 ) {
526 fprintf( stderr, "%s: -U incompatible with version %d\n",
530 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
531 fprintf( stderr, "%s: incompatible previous "
532 "authentication choice\n",
536 authmethod = LDAP_AUTH_SASL;
537 version = LDAP_VERSION3;
538 sasl_authc_id = strdup( optarg );
540 fprintf( stderr, "%s: not compiled with SASL support\n",
542 return( EXIT_FAILURE );
545 case 'v': /* verbose mode */
548 case 'w': /* password */
549 passwd.bv_val = strdup( optarg );
553 for( p = optarg; *p != '\0'; p++ ) {
557 passwd.bv_len = strlen( passwd.bv_val );
563 #ifdef HAVE_CYRUS_SASL
564 if( sasl_mech != NULL ) {
565 fprintf( stderr, "%s: -Y previously specified\n", prog );
568 if( version == LDAP_VERSION2 ) {
569 fprintf( stderr, "%s: -Y incompatible with version %d\n",
573 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
574 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
577 authmethod = LDAP_AUTH_SASL;
578 version = LDAP_VERSION3;
579 sasl_mech = strdup( optarg );
581 fprintf( stderr, "%s: not compiled with SASL support\n",
583 return( EXIT_FAILURE );
587 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
588 fprintf( stderr, "%s: incompatible with previous "
589 "authentication choice\n", prog );
592 authmethod = LDAP_AUTH_SIMPLE;
595 #ifdef HAVE_CYRUS_SASL
596 if( sasl_authz_id != NULL ) {
597 fprintf( stderr, "%s: -X previously specified\n", prog );
600 if( version == LDAP_VERSION2 ) {
601 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
605 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
606 fprintf( stderr, "%s: -X incompatible with "
607 "authentication choice\n", prog );
610 authmethod = LDAP_AUTH_SASL;
611 version = LDAP_VERSION3;
612 sasl_authz_id = strdup( optarg );
614 fprintf( stderr, "%s: not compiled with SASL support\n",
616 return( EXIT_FAILURE );
621 if( version == LDAP_VERSION2 ) {
622 fprintf( stderr, "%s: -Z incompatible with version %d\n",
626 version = LDAP_VERSION3;
629 fprintf( stderr, "%s: not compiled with TLS support\n",
631 return( EXIT_FAILURE );
635 fprintf( stderr, "%s: unrecognized option -%c\n",
642 version = LDAP_VERSION3;
644 if (authmethod == -1 && version > LDAP_VERSION2) {
645 #ifdef HAVE_CYRUS_SASL
646 authmethod = LDAP_AUTH_SASL;
648 authmethod = LDAP_AUTH_SIMPLE;
652 if (( argc - optind < 1 ) ||
653 ( *argv[optind] != '(' /*')'*/ &&
654 ( strchr( argv[optind], '=' ) == NULL ) ) )
656 filtpattern = "(objectclass=*)";
658 filtpattern = strdup( argv[optind++] );
661 if ( argv[optind] != NULL ) {
662 attrs = &argv[optind];
665 if ( infile != NULL ) {
666 if ( infile[0] == '-' && infile[1] == '\0' ) {
668 } else if (( fp = fopen( infile, "r" )) == NULL ) {
674 if ( tmpdir == NULL ) {
677 if ( urlpre == NULL )
681 if( urlpre == NULL ) {
682 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
684 if( urlpre == NULL ) {
689 sprintf( urlpre, "file:///%s/",
690 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
696 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
697 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
699 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
700 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
706 (void) SIGNAL( SIGPIPE, SIG_IGN );
709 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
711 fprintf( stderr, "ldap_init( %s, %d )\n",
712 ldaphost != NULL ? ldaphost : "<DEFAULT>",
716 ld = ldap_init( ldaphost, ldapport );
718 perror("ldapsearch: ldap_init");
724 fprintf( stderr, "ldap_initialize( %s )\n",
725 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
728 rc = ldap_initialize( &ld, ldapuri );
729 if( rc != LDAP_SUCCESS ) {
730 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
731 rc, ldap_err2string(rc) );
737 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
739 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
742 if (timelimit != -1 &&
743 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
745 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
748 if (sizelimit != -1 &&
749 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
751 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
756 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
757 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
759 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
760 referrals ? "on" : "off" );
764 if (version == -1 ) {
765 version = LDAP_VERSION3;
768 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
769 != LDAP_OPT_SUCCESS )
771 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
776 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
777 ldap_perror( ld, "ldap_start_tls" );
784 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
785 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
788 if ( authmethod == LDAP_AUTH_SASL ) {
789 #ifdef HAVE_CYRUS_SASL
792 if( sasl_secprops != NULL ) {
793 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
794 (void *) sasl_secprops );
796 if( rc != LDAP_OPT_SUCCESS ) {
798 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
800 return( EXIT_FAILURE );
804 defaults = lutil_sasl_defaults( ld,
811 rc = ldap_sasl_interactive_bind_s( ld, binddn,
812 sasl_mech, NULL, NULL,
813 sasl_flags, lutil_sasl_interact, defaults );
815 if( rc != LDAP_SUCCESS ) {
816 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
817 return( EXIT_FAILURE );
820 fprintf( stderr, "%s: not compiled with SASL support\n",
822 return( EXIT_FAILURE );
825 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
827 ldap_perror( ld, "ldap_bind" );
828 return( EXIT_FAILURE );
835 LDAPControl *ctrls[2];
839 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
840 c.ldctl_value.bv_val = NULL;
841 c.ldctl_value.bv_len = 0;
842 c.ldctl_iscritical = manageDSAit > 1;
844 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
846 if( err != LDAP_OPT_SUCCESS ) {
847 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
848 c.ldctl_iscritical ? "critical " : "" );
849 if( c.ldctl_iscritical ) {
850 exit( EXIT_FAILURE );
856 fprintf( stderr, "filter%s: %s\nrequesting: ",
857 infile != NULL ? " pattern" : "",
860 if ( attrs == NULL ) {
861 fprintf( stderr, "ALL" );
863 for ( i = 0; attrs[ i ] != NULL; ++i ) {
864 fprintf( stderr, "%s ", attrs[ i ] );
867 fprintf( stderr, "\n" );
871 printf( "# extended LDIF\n" );
872 } else if ( ldif < 3 ) {
873 printf( "version: %d\n\n", 1 );
877 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
879 infile != NULL ? " pattern" : "",
882 if ( attrs == NULL ) {
885 for ( i = 0; attrs[ i ] != NULL; ++i ) {
886 printf( "%s ", attrs[ i ] );
891 printf("\n# with manageDSAit %scontrol",
892 manageDSAit > 1 ? "critical " : "" );
898 if ( infile == NULL ) {
899 rc = dosearch( ld, base, scope, NULL, filtpattern,
900 attrs, attrsonly, NULL, NULL, NULL, -1 );
905 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
906 line[ strlen( line ) - 1 ] = '\0';
912 rc = dosearch( ld, base, scope, filtpattern, line,
913 attrs, attrsonly, NULL, NULL, NULL, -1 );
933 LDAPControl **sctrls,
934 LDAPControl **cctrls,
935 struct timeval *timeout,
938 char filter[ BUFSIZ ];
945 LDAPMessage *res, *msg;
948 if( filtpatt != NULL ) {
949 sprintf( filter, filtpatt, value );
952 fprintf( stderr, "filter: %s\n", filter );
956 printf( "#\n# filter: %s\n#\n", filter );
960 sprintf( filter, "%s", value );
967 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
968 sctrls, cctrls, timeout, sizelimit, &msgid );
970 if( rc != LDAP_SUCCESS ) {
971 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
972 prog, ldap_err2string( rc ), rc );
976 nresponses = nentries = nreferences = nextended = npartial = 0;
980 while ((rc = ldap_result( ld, LDAP_RES_ANY,
981 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
985 (void) ldap_sort_entries( ld, &res,
986 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
989 for ( msg = ldap_first_message( ld, res );
991 msg = ldap_next_message( ld, msg ) )
993 if( nresponses++ ) putchar('\n');
995 switch( ldap_msgtype( msg ) ) {
996 case LDAP_RES_SEARCH_ENTRY:
998 print_entry( ld, msg, attrsonly );
1001 case LDAP_RES_SEARCH_REFERENCE:
1003 print_reference( ld, msg );
1006 case LDAP_RES_EXTENDED:
1008 print_extended( ld, msg );
1010 if( ldap_msgid( msg ) == 0 ) {
1011 /* unsolicited extended operation */
1016 case LDAP_RES_EXTENDED_PARTIAL:
1018 print_partial( ld, msg );
1021 case LDAP_RES_SEARCH_RESULT:
1022 rc = print_result( ld, msg, 1 );
1027 ldap_msgfree( res );
1031 ldap_perror( ld, "ldap_result" );
1037 printf( "\n# numResponses: %d\n", nresponses );
1038 if( nentries ) printf( "# numEntries: %d\n", nentries );
1039 if( nextended ) printf( "# numExtended: %d\n", nextended );
1040 if( npartial ) printf( "# numPartial: %d\n", npartial );
1041 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1054 char tmpfname[ 256 ];
1057 BerElement *ber = NULL;
1058 struct berval **bvals;
1059 LDAPControl **ctrls = NULL;
1062 dn = ldap_get_dn( ld, entry );
1066 ufn = ldap_dn2ufn( dn );
1067 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1069 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1071 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1073 if( rc != LDAP_SUCCESS ) {
1074 fprintf(stderr, "print_entry: %d\n", rc );
1075 ldap_perror( ld, "ldap_get_entry_controls" );
1076 exit( EXIT_FAILURE );
1080 print_ctrls( ctrls );
1081 ldap_controls_free( ctrls );
1086 ufn = ldap_dn2ufn( dn );
1088 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1091 if( ufn != NULL ) ldap_memfree( ufn );
1094 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1095 a = ldap_next_attribute( ld, entry, ber ) )
1098 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1100 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1101 for ( i = 0; bvals[i] != NULL; i++ ) {
1102 if ( vals2tmp > 1 || ( vals2tmp
1103 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1106 /* write value to file */
1107 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1111 tmpfd = mkstemp( tmpfname );
1118 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1123 if ( fwrite( bvals[ i ]->bv_val,
1124 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1133 sprintf( url, "%s%s", urlpre,
1134 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1137 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1140 write_ldif( LDIF_PUT_VALUE, a,
1141 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1144 ber_bvecfree( bvals );
1153 static void print_reference(
1155 LDAPMessage *reference )
1159 LDAPControl **ctrls;
1162 printf("# search reference\n");
1165 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1167 if( rc != LDAP_SUCCESS ) {
1168 ldap_perror(ld, "ldap_parse_reference");
1169 exit( EXIT_FAILURE );
1174 for( i=0; refs[i] != NULL; i++ ) {
1175 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1176 "ref", refs[i], strlen(refs[i]) );
1178 ber_memvfree( (void **) refs );
1182 print_ctrls( ctrls );
1183 ldap_controls_free( ctrls );
1187 static void print_extended(
1189 LDAPMessage *extended )
1192 char *retoid = NULL;
1193 struct berval *retdata = NULL;
1196 printf("# extended result response\n");
1199 rc = ldap_parse_extended_result( ld, extended,
1200 &retoid, &retdata, 0 );
1202 if( rc != LDAP_SUCCESS ) {
1203 ldap_perror(ld, "ldap_parse_extended_result");
1204 exit( EXIT_FAILURE );
1207 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1208 "extended", retoid, retoid ? strlen(retoid) : 0 );
1209 ber_memfree( retoid );
1212 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1213 "data", retdata->bv_val, retdata->bv_len );
1214 ber_bvfree( retdata );
1217 print_result( ld, extended, 0 );
1220 static void print_partial(
1222 LDAPMessage *partial )
1225 char *retoid = NULL;
1226 struct berval *retdata = NULL;
1227 LDAPControl **ctrls = NULL;
1230 printf("# extended partial response\n");
1233 rc = ldap_parse_extended_partial( ld, partial,
1234 &retoid, &retdata, &ctrls, 0 );
1236 if( rc != LDAP_SUCCESS ) {
1237 ldap_perror(ld, "ldap_parse_extended_partial");
1238 exit( EXIT_FAILURE );
1241 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1242 "partial", retoid, retoid ? strlen(retoid) : 0 );
1244 ber_memfree( retoid );
1247 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1249 retdata->bv_val, retdata->bv_len );
1251 ber_bvfree( retdata );
1255 print_ctrls( ctrls );
1256 ldap_controls_free( ctrls );
1260 static int print_result(
1262 LDAPMessage *result, int search )
1266 char *matcheddn = NULL;
1269 LDAPControl **ctrls = NULL;
1273 printf("# search result\n");
1276 printf("%s: %d\n", "search", ldap_msgid(result) );
1280 rc = ldap_parse_result( ld, result,
1281 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1283 if( rc != LDAP_SUCCESS ) {
1284 ldap_perror(ld, "ldap_parse_result");
1285 exit( EXIT_FAILURE );
1290 printf( "result: %d %s\n", err, ldap_err2string(err) );
1292 } else if ( err != LDAP_SUCCESS ) {
1293 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1296 if( matcheddn && *matcheddn ) {
1298 write_ldif( LDIF_PUT_VALUE,
1299 "matchedDN", matcheddn, strlen(matcheddn) );
1301 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1304 ber_memfree( matcheddn );
1307 if( text && *text ) {
1309 write_ldif( LDIF_PUT_TEXT, "text",
1310 text, strlen(text) );
1312 fprintf( stderr, "Additional information: %s\n", text );
1315 ber_memfree( text );
1320 for( i=0; refs[i] != NULL; i++ ) {
1322 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1324 fprintf( stderr, "Referral: %s\n", refs[i] );
1328 ber_memvfree( (void **) refs );
1332 print_ctrls( ctrls );
1333 ldap_controls_free( ctrls );
1339 static void print_ctrls(
1340 LDAPControl **ctrls )
1343 for(i=0; ctrls[i] != NULL; i++ ) {
1344 /* control: OID criticality base64value */
1345 struct berval *b64 = NULL;
1349 len = strlen( ctrls[i]->ldctl_oid );
1351 /* add enough for space after OID and the critical value itself */
1352 len += ctrls[i]->ldctl_iscritical
1353 ? sizeof("true") : sizeof("false");
1355 /* convert to base64 */
1356 if( ctrls[i]->ldctl_value.bv_len ) {
1357 b64 = ber_memalloc( sizeof(struct berval) );
1359 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1360 ctrls[i]->ldctl_value.bv_len ) + 1;
1361 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1363 b64->bv_len = lutil_b64_ntop(
1364 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1365 b64->bv_val, b64->bv_len );
1369 len += 1 + b64->bv_len;
1372 str = malloc( len + 1 );
1373 strcpy( str, ctrls[i]->ldctl_oid );
1374 strcat( str, ctrls[i]->ldctl_iscritical
1375 ? " true" : " false" );
1379 strcat(str, b64->bv_val );
1382 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1383 "control", str, len );
1391 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1395 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1399 fputs( ldif, stdout );
1400 ber_memfree( ldif );