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 != '/') {
171 for (p = url; *p; p++) {
172 if (*p == *LDAP_DIRSEP)
179 main( int argc, char **argv )
181 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
183 int rc, i, first, scope, deref, attrsonly, manageDSAit;
184 int referrals, timelimit, sizelimit, debug;
185 int authmethod, version, want_bindpw;
187 int valuesReturnFilter;
189 struct berval *bvalp = NULL;
190 char *vrFilter = NULL, *control = NULL, *s;
194 debug = verbose = not = vals2tmp = referrals = valuesReturnFilter =
195 attrsonly = manageDSAit = ldif = want_bindpw = 0;
197 prog = lutil_progname( "ldapsearch", argc, argv );
199 lutil_log_initialize(argc, argv);
201 deref = sizelimit = timelimit = version = -1;
203 scope = LDAP_SCOPE_SUBTREE;
206 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
207 (def_tmpdir = getenv("TMP")) == NULL &&
208 (def_tmpdir = getenv("TEMP")) == NULL )
210 def_tmpdir = LDAP_TMPDIR;
214 def_tmpdir = LDAP_TMPDIR;
216 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
218 if( def_urlpre == NULL ) {
223 sprintf( def_urlpre, "file:///%s/",
224 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
226 urlize( def_urlpre );
228 while (( i = getopt( argc, argv, "Aa:b:E:F:f:Ll:S:s:T:tuz:"
229 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z")) != EOF )
233 case 'a': /* set alias deref option */
234 if ( strcasecmp( optarg, "never" ) == 0 ) {
235 deref = LDAP_DEREF_NEVER;
236 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
237 deref = LDAP_DEREF_SEARCHING;
238 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
239 deref = LDAP_DEREF_FINDING;
240 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
241 deref = LDAP_DEREF_ALWAYS;
243 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
247 case 'A': /* retrieve attribute names only -- no values */
250 case 'b': /* search base */
251 base = strdup( optarg );
253 case 'f': /* input file */
254 if( infile != NULL ) {
255 fprintf( stderr, "%s: -f previously specified\n", prog );
258 infile = strdup( optarg );
260 case 'E': /* controls */
261 if( version == LDAP_VERSION2 ) {
262 fprintf( stderr, "%s: -C incompatible with LDAPv%d\n",
267 /* should be extended to support comma separated list of
268 * key/value pairs: -E foo=123,bar=567
271 control = strdup( optarg );
272 if ( (s = strchr( control, '=' )) == NULL ) {
277 if ( strcasecmp( control, "mv" ) == 0 ) {
278 /* ValuesReturnFilter control */
279 if (valuesReturnFilter!=0) {
280 fprintf( stderr, "ValuesReturnFilter previously specified");
286 valuesReturnFilter=2;
288 valuesReturnFilter=1;
292 version = LDAP_VERSION3;
296 fprintf( stderr, "Invalid control name: %s\n", control );
301 case 'F': /* uri prefix */
302 if( urlpre ) free( urlpre );
303 urlpre = strdup( optarg );
305 case 'l': /* time limit */
306 timelimit = atoi( optarg );
307 if( timelimit < 0 ) {
308 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
313 case 'L': /* print entries in LDIF format */
316 case 's': /* search scope */
317 if ( strcasecmp( optarg, "base" ) == 0 ) {
318 scope = LDAP_SCOPE_BASE;
319 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
320 scope = LDAP_SCOPE_ONELEVEL;
321 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
322 scope = LDAP_SCOPE_SUBTREE;
324 fprintf( stderr, "scope should be base, one, or sub\n" );
328 case 'S': /* sort attribute */
329 sortattr = strdup( optarg );
331 case 'u': /* include UFN */
334 case 't': /* write attribute values to TMPDIR files */
337 case 'T': /* tmpdir */
338 if( tmpdir ) free( tmpdir );
339 tmpdir = strdup( optarg );
341 case 'z': /* size limit */
342 sizelimit = atoi( optarg );
350 debug |= atoi( optarg );
352 case 'D': /* bind DN */
353 if( binddn != NULL ) {
354 fprintf( stderr, "%s: -D previously specified\n", prog );
357 binddn = strdup( optarg );
359 case 'h': /* ldap host */
360 if( ldapuri != NULL ) {
361 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
364 if( ldaphost != NULL ) {
365 fprintf( stderr, "%s: -h previously specified\n", prog );
368 ldaphost = strdup( optarg );
370 case 'H': /* ldap URI */
371 if( ldaphost != NULL ) {
372 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
376 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
379 if( ldapuri != NULL ) {
380 fprintf( stderr, "%s: -H previously specified\n", prog );
383 ldapuri = strdup( optarg );
386 #ifdef HAVE_CYRUS_SASL
387 if( version == LDAP_VERSION2 ) {
388 fprintf( stderr, "%s: -I incompatible with version %d\n",
392 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
393 fprintf( stderr, "%s: incompatible previous "
394 "authentication choice\n",
398 authmethod = LDAP_AUTH_SASL;
399 version = LDAP_VERSION3;
400 sasl_flags = LDAP_SASL_INTERACTIVE;
403 fprintf( stderr, "%s: was not compiled with SASL support\n",
405 return( EXIT_FAILURE );
407 case 'k': /* kerberos bind */
408 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
409 if( version > LDAP_VERSION2 ) {
410 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
415 if( authmethod != -1 ) {
416 fprintf( stderr, "%s: -k incompatible with previous "
417 "authentication choice\n", prog );
421 authmethod = LDAP_AUTH_KRBV4;
423 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
427 case 'K': /* kerberos bind, part one only */
428 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
429 if( version > LDAP_VERSION2 ) {
430 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
434 if( authmethod != -1 ) {
435 fprintf( stderr, "%s: incompatible with previous "
436 "authentication choice\n", prog );
440 authmethod = LDAP_AUTH_KRBV41;
442 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
443 return( EXIT_FAILURE );
447 /* enable Manage DSA IT */
448 if( version == LDAP_VERSION2 ) {
449 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
454 version = LDAP_VERSION3;
456 case 'n': /* print deletes, don't actually do them */
460 #ifdef HAVE_CYRUS_SASL
461 if( sasl_secprops != NULL ) {
462 fprintf( stderr, "%s: -O previously specified\n", prog );
465 if( version == LDAP_VERSION2 ) {
466 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
470 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
471 fprintf( stderr, "%s: incompatible previous "
472 "authentication choice\n", prog );
475 authmethod = LDAP_AUTH_SASL;
476 version = LDAP_VERSION3;
477 sasl_secprops = strdup( optarg );
479 fprintf( stderr, "%s: not compiled with SASL support\n",
481 return( EXIT_FAILURE );
486 fprintf( stderr, "%s: -p previously specified\n", prog );
489 ldapport = atoi( optarg );
492 switch( atoi(optarg) ) {
494 if( version == LDAP_VERSION3 ) {
495 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
499 version = LDAP_VERSION2;
502 if( version == LDAP_VERSION2 ) {
503 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
507 version = LDAP_VERSION3;
510 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
513 return( EXIT_FAILURE );
516 #ifdef HAVE_CYRUS_SASL
517 if( version == LDAP_VERSION2 ) {
518 fprintf( stderr, "%s: -Q incompatible with version %d\n",
522 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
523 fprintf( stderr, "%s: incompatible previous "
524 "authentication choice\n",
528 authmethod = LDAP_AUTH_SASL;
529 version = LDAP_VERSION3;
530 sasl_flags = LDAP_SASL_QUIET;
533 fprintf( stderr, "%s: not compiled with SASL support\n",
535 return( EXIT_FAILURE );
538 #ifdef HAVE_CYRUS_SASL
539 if( sasl_realm != NULL ) {
540 fprintf( stderr, "%s: -R previously specified\n", prog );
543 if( version == LDAP_VERSION2 ) {
544 fprintf( stderr, "%s: -R incompatible with version %d\n",
548 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
549 fprintf( stderr, "%s: incompatible previous "
550 "authentication choice\n",
554 authmethod = LDAP_AUTH_SASL;
555 version = LDAP_VERSION3;
556 sasl_realm = strdup( optarg );
558 fprintf( stderr, "%s: not compiled with SASL support\n",
560 return( EXIT_FAILURE );
564 #ifdef HAVE_CYRUS_SASL
565 if( sasl_authc_id != NULL ) {
566 fprintf( stderr, "%s: -U previously specified\n", prog );
569 if( version == LDAP_VERSION2 ) {
570 fprintf( stderr, "%s: -U incompatible with version %d\n",
574 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
575 fprintf( stderr, "%s: incompatible previous "
576 "authentication choice\n",
580 authmethod = LDAP_AUTH_SASL;
581 version = LDAP_VERSION3;
582 sasl_authc_id = strdup( optarg );
584 fprintf( stderr, "%s: not compiled with SASL support\n",
586 return( EXIT_FAILURE );
589 case 'v': /* verbose mode */
592 case 'w': /* password */
593 passwd.bv_val = strdup( optarg );
597 for( p = optarg; *p != '\0'; p++ ) {
601 passwd.bv_len = strlen( passwd.bv_val );
607 #ifdef HAVE_CYRUS_SASL
608 if( sasl_mech != NULL ) {
609 fprintf( stderr, "%s: -Y previously specified\n", prog );
612 if( version == LDAP_VERSION2 ) {
613 fprintf( stderr, "%s: -Y incompatible with version %d\n",
617 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
618 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
621 authmethod = LDAP_AUTH_SASL;
622 version = LDAP_VERSION3;
623 sasl_mech = strdup( optarg );
625 fprintf( stderr, "%s: not compiled with SASL support\n",
627 return( EXIT_FAILURE );
631 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
632 fprintf( stderr, "%s: incompatible with previous "
633 "authentication choice\n", prog );
636 authmethod = LDAP_AUTH_SIMPLE;
639 #ifdef HAVE_CYRUS_SASL
640 if( sasl_authz_id != NULL ) {
641 fprintf( stderr, "%s: -X previously specified\n", prog );
644 if( version == LDAP_VERSION2 ) {
645 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
649 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
650 fprintf( stderr, "%s: -X incompatible with "
651 "authentication choice\n", prog );
654 authmethod = LDAP_AUTH_SASL;
655 version = LDAP_VERSION3;
656 sasl_authz_id = strdup( optarg );
658 fprintf( stderr, "%s: not compiled with SASL support\n",
660 return( EXIT_FAILURE );
665 if( version == LDAP_VERSION2 ) {
666 fprintf( stderr, "%s: -Z incompatible with version %d\n",
670 version = LDAP_VERSION3;
673 fprintf( stderr, "%s: not compiled with TLS support\n",
675 return( EXIT_FAILURE );
679 fprintf( stderr, "%s: unrecognized option -%c\n",
686 version = LDAP_VERSION3;
688 if (authmethod == -1 && version > LDAP_VERSION2) {
689 #ifdef HAVE_CYRUS_SASL
690 authmethod = LDAP_AUTH_SASL;
692 authmethod = LDAP_AUTH_SIMPLE;
696 if (( argc - optind < 1 ) ||
697 ( *argv[optind] != '(' /*')'*/ &&
698 ( strchr( argv[optind], '=' ) == NULL ) ) )
700 filtpattern = "(objectclass=*)";
702 filtpattern = strdup( argv[optind++] );
705 if ( argv[optind] != NULL ) {
706 attrs = &argv[optind];
709 if ( infile != NULL ) {
710 if ( infile[0] == '-' && infile[1] == '\0' ) {
712 } else if (( fp = fopen( infile, "r" )) == NULL ) {
718 if ( tmpdir == NULL ) {
721 if ( urlpre == NULL )
725 if( urlpre == NULL ) {
726 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
728 if( urlpre == NULL ) {
733 sprintf( urlpre, "file:///%s/",
734 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
740 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
741 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
743 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
744 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
750 (void) SIGNAL( SIGPIPE, SIG_IGN );
753 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
755 fprintf( stderr, "ldap_init( %s, %d )\n",
756 ldaphost != NULL ? ldaphost : "<DEFAULT>",
760 ld = ldap_init( ldaphost, ldapport );
762 perror("ldapsearch: ldap_init");
768 fprintf( stderr, "ldap_initialize( %s )\n",
769 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
772 rc = ldap_initialize( &ld, ldapuri );
773 if( rc != LDAP_SUCCESS ) {
774 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
775 rc, ldap_err2string(rc) );
781 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
783 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
786 if (timelimit != -1 &&
787 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
789 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
792 if (sizelimit != -1 &&
793 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
795 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
800 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
801 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
803 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
804 referrals ? "on" : "off" );
808 if (version == -1 ) {
809 version = LDAP_VERSION3;
812 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
813 != LDAP_OPT_SUCCESS )
815 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
820 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
821 ldap_perror( ld, "ldap_start_tls" );
828 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
829 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
832 if ( authmethod == LDAP_AUTH_SASL ) {
833 #ifdef HAVE_CYRUS_SASL
836 if( sasl_secprops != NULL ) {
837 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
838 (void *) sasl_secprops );
840 if( rc != LDAP_OPT_SUCCESS ) {
842 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
844 return( EXIT_FAILURE );
848 defaults = lutil_sasl_defaults( ld,
855 rc = ldap_sasl_interactive_bind_s( ld, binddn,
856 sasl_mech, NULL, NULL,
857 sasl_flags, lutil_sasl_interact, defaults );
859 if( rc != LDAP_SUCCESS ) {
860 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
861 return( EXIT_FAILURE );
864 fprintf( stderr, "%s: not compiled with SASL support\n",
866 return( EXIT_FAILURE );
869 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
871 ldap_perror( ld, "ldap_bind" );
872 return( EXIT_FAILURE );
876 if ( manageDSAit || valuesReturnFilter ) {
880 LDAPControl *ctrls[3];
886 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
887 c1.ldctl_value.bv_val = NULL;
888 c1.ldctl_value.bv_len = 0;
889 c1.ldctl_iscritical = manageDSAit > 1;
892 if ( valuesReturnFilter ) {
896 c2.ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
897 c2.ldctl_iscritical = valuesReturnFilter > 1;
899 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
903 if ( err = ldap_put_vrFilter(ber, vrFilter)==-1 ) {
905 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
909 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
913 c2.ldctl_value=(*bvalp);
916 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
921 if( err != LDAP_OPT_SUCCESS ) {
922 fprintf( stderr, "Could not set %scontrols\n",
923 (c1.ldctl_iscritical || c2.ldctl_iscritical)
924 ? "critical " : "" );
925 if( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
932 fprintf( stderr, "filter%s: %s\nrequesting: ",
933 infile != NULL ? " pattern" : "",
936 if ( attrs == NULL ) {
937 fprintf( stderr, "ALL" );
939 for ( i = 0; attrs[ i ] != NULL; ++i ) {
940 fprintf( stderr, "%s ", attrs[ i ] );
943 fprintf( stderr, "\n" );
947 printf( "# extended LDIF\n" );
948 } else if ( ldif < 3 ) {
949 printf( "version: %d\n\n", 1 );
953 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
955 infile != NULL ? " pattern" : "",
958 if ( attrs == NULL ) {
961 for ( i = 0; attrs[ i ] != NULL; ++i ) {
962 printf( "%s ", attrs[ i ] );
967 printf("\n# with manageDSAit %scontrol",
968 manageDSAit > 1 ? "critical " : "" );
974 if ( infile == NULL ) {
975 rc = dosearch( ld, base, scope, NULL, filtpattern,
976 attrs, attrsonly, NULL, NULL, NULL, -1 );
981 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
982 line[ strlen( line ) - 1 ] = '\0';
988 rc = dosearch( ld, base, scope, filtpattern, line,
989 attrs, attrsonly, NULL, NULL, NULL, -1 );
1001 static int dosearch(
1009 LDAPControl **sctrls,
1010 LDAPControl **cctrls,
1011 struct timeval *timeout,
1021 LDAPMessage *res, *msg;
1024 if( filtpatt != NULL ) {
1025 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1026 if( filter == NULL ) {
1028 return EXIT_FAILURE;
1031 sprintf( filter, filtpatt, value );
1034 fprintf( stderr, "filter: %s\n", filter );
1038 printf( "#\n# filter: %s\n#\n", filter );
1046 return LDAP_SUCCESS;
1049 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1050 sctrls, cctrls, timeout, sizelimit, &msgid );
1052 if ( filtpatt != NULL ) {
1056 if( rc != LDAP_SUCCESS ) {
1057 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1058 prog, ldap_err2string( rc ), rc );
1062 nresponses = nentries = nreferences = nextended = npartial = 0;
1066 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1067 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1071 (void) ldap_sort_entries( ld, &res,
1072 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1075 for ( msg = ldap_first_message( ld, res );
1077 msg = ldap_next_message( ld, msg ) )
1079 if( nresponses++ ) putchar('\n');
1081 switch( ldap_msgtype( msg ) ) {
1082 case LDAP_RES_SEARCH_ENTRY:
1084 print_entry( ld, msg, attrsonly );
1087 case LDAP_RES_SEARCH_REFERENCE:
1089 print_reference( ld, msg );
1092 case LDAP_RES_EXTENDED:
1094 print_extended( ld, msg );
1096 if( ldap_msgid( msg ) == 0 ) {
1097 /* unsolicited extended operation */
1102 case LDAP_RES_EXTENDED_PARTIAL:
1104 print_partial( ld, msg );
1107 case LDAP_RES_SEARCH_RESULT:
1108 rc = print_result( ld, msg, 1 );
1113 ldap_msgfree( res );
1117 ldap_perror( ld, "ldap_result" );
1123 printf( "\n# numResponses: %d\n", nresponses );
1124 if( nentries ) printf( "# numEntries: %d\n", nentries );
1125 if( nextended ) printf( "# numExtended: %d\n", nextended );
1126 if( npartial ) printf( "# numPartial: %d\n", npartial );
1127 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1140 char tmpfname[ 256 ];
1143 BerElement *ber = NULL;
1144 struct berval **bvals;
1145 LDAPControl **ctrls = NULL;
1148 dn = ldap_get_dn( ld, entry );
1152 ufn = ldap_dn2ufn( dn );
1153 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1155 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1157 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1159 if( rc != LDAP_SUCCESS ) {
1160 fprintf(stderr, "print_entry: %d\n", rc );
1161 ldap_perror( ld, "ldap_get_entry_controls" );
1162 exit( EXIT_FAILURE );
1166 print_ctrls( ctrls );
1167 ldap_controls_free( ctrls );
1172 ufn = ldap_dn2ufn( dn );
1174 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1177 if( ufn != NULL ) ldap_memfree( ufn );
1180 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1181 a = ldap_next_attribute( ld, entry, ber ) )
1184 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1186 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1187 for ( i = 0; bvals[i] != NULL; i++ ) {
1188 if ( vals2tmp > 1 || ( vals2tmp
1189 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1192 /* write value to file */
1193 snprintf( tmpfname, sizeof tmpfname,
1194 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1198 tmpfd = mkstemp( tmpfname );
1205 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1210 if ( fwrite( bvals[ i ]->bv_val,
1211 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1220 snprintf( url, sizeof url, "%s%s", urlpre,
1221 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1224 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1227 write_ldif( LDIF_PUT_VALUE, a,
1228 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1231 ber_bvecfree( bvals );
1240 static void print_reference(
1242 LDAPMessage *reference )
1246 LDAPControl **ctrls;
1249 printf("# search reference\n");
1252 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1254 if( rc != LDAP_SUCCESS ) {
1255 ldap_perror(ld, "ldap_parse_reference");
1256 exit( EXIT_FAILURE );
1261 for( i=0; refs[i] != NULL; i++ ) {
1262 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1263 "ref", refs[i], strlen(refs[i]) );
1265 ber_memvfree( (void **) refs );
1269 print_ctrls( ctrls );
1270 ldap_controls_free( ctrls );
1274 static void print_extended(
1276 LDAPMessage *extended )
1279 char *retoid = NULL;
1280 struct berval *retdata = NULL;
1283 printf("# extended result response\n");
1286 rc = ldap_parse_extended_result( ld, extended,
1287 &retoid, &retdata, 0 );
1289 if( rc != LDAP_SUCCESS ) {
1290 ldap_perror(ld, "ldap_parse_extended_result");
1291 exit( EXIT_FAILURE );
1294 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1295 "extended", retoid, retoid ? strlen(retoid) : 0 );
1296 ber_memfree( retoid );
1299 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1300 "data", retdata->bv_val, retdata->bv_len );
1301 ber_bvfree( retdata );
1304 print_result( ld, extended, 0 );
1307 static void print_partial(
1309 LDAPMessage *partial )
1312 char *retoid = NULL;
1313 struct berval *retdata = NULL;
1314 LDAPControl **ctrls = NULL;
1317 printf("# extended partial response\n");
1320 rc = ldap_parse_extended_partial( ld, partial,
1321 &retoid, &retdata, &ctrls, 0 );
1323 if( rc != LDAP_SUCCESS ) {
1324 ldap_perror(ld, "ldap_parse_extended_partial");
1325 exit( EXIT_FAILURE );
1328 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1329 "partial", retoid, retoid ? strlen(retoid) : 0 );
1331 ber_memfree( retoid );
1334 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1336 retdata->bv_val, retdata->bv_len );
1338 ber_bvfree( retdata );
1342 print_ctrls( ctrls );
1343 ldap_controls_free( ctrls );
1347 static int print_result(
1349 LDAPMessage *result, int search )
1353 char *matcheddn = NULL;
1356 LDAPControl **ctrls = NULL;
1360 printf("# search result\n");
1363 printf("%s: %d\n", "search", ldap_msgid(result) );
1367 rc = ldap_parse_result( ld, result,
1368 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1370 if( rc != LDAP_SUCCESS ) {
1371 ldap_perror(ld, "ldap_parse_result");
1372 exit( EXIT_FAILURE );
1377 printf( "result: %d %s\n", err, ldap_err2string(err) );
1379 } else if ( err != LDAP_SUCCESS ) {
1380 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1383 if( matcheddn && *matcheddn ) {
1385 write_ldif( LDIF_PUT_VALUE,
1386 "matchedDN", matcheddn, strlen(matcheddn) );
1388 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1391 ber_memfree( matcheddn );
1394 if( text && *text ) {
1396 write_ldif( LDIF_PUT_TEXT, "text",
1397 text, strlen(text) );
1399 fprintf( stderr, "Additional information: %s\n", text );
1402 ber_memfree( text );
1407 for( i=0; refs[i] != NULL; i++ ) {
1409 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1411 fprintf( stderr, "Referral: %s\n", refs[i] );
1415 ber_memvfree( (void **) refs );
1419 print_ctrls( ctrls );
1420 ldap_controls_free( ctrls );
1426 static void print_ctrls(
1427 LDAPControl **ctrls )
1430 for(i=0; ctrls[i] != NULL; i++ ) {
1431 /* control: OID criticality base64value */
1432 struct berval *b64 = NULL;
1436 len = strlen( ctrls[i]->ldctl_oid );
1438 /* add enough for space after OID and the critical value itself */
1439 len += ctrls[i]->ldctl_iscritical
1440 ? sizeof("true") : sizeof("false");
1442 /* convert to base64 */
1443 if( ctrls[i]->ldctl_value.bv_len ) {
1444 b64 = ber_memalloc( sizeof(struct berval) );
1446 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1447 ctrls[i]->ldctl_value.bv_len ) + 1;
1448 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1450 b64->bv_len = lutil_b64_ntop(
1451 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1452 b64->bv_val, b64->bv_len );
1456 len += 1 + b64->bv_len;
1459 str = malloc( len + 1 );
1460 strcpy( str, ctrls[i]->ldctl_oid );
1461 strcat( str, ctrls[i]->ldctl_iscritical
1462 ? " true" : " false" );
1466 strcat(str, b64->bv_val );
1469 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1470 "control", str, len );
1478 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1482 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1486 fputs( ldif, stdout );
1487 ber_memfree( ldif );