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 " -E [!]<ctrl>[=<ctrlparam>] search controls (! indicates criticality)\n"
58 " [!]mv=<filter> (matched values filter)\n"
59 " -F prefix URL prefix for files (default: %s)\n"
60 " -l limit time limit (in seconds) for search\n"
61 " -L print responses in LDIFv1 format\n"
62 " -LL print responses in LDIF format without comments\n"
63 " -LLL print responses in LDIF format without comments\n"
65 " -s scope one of base, one, or sub (search scope)\n"
66 " -S attr sort the results by attribute `attr'\n"
67 " -t write binary values to files in temporary directory\n"
68 " -tt write all values to files in temporary directory\n"
69 " -T path write files to directory specified by path (default: %s)\n"
70 " -u include User Friendly entry names in the output\n"
71 " -z limit size limit (in entries) for search\n"
74 " -d level set LDAP debugging level to `level'\n"
75 " -D binddn bind DN\n"
76 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
77 " [!]manageDSAit (alternate form, see -M)\n"
79 " -f file read operations from `file'\n"
80 " -h host LDAP server\n"
81 " -H URI LDAP Uniform Resource Indentifier(s)\n"
82 " -I use SASL Interactive mode\n"
83 " -k use Kerberos authentication\n"
84 " -K like -k, but do only step 1 of the Kerberos bind\n"
85 " -M enable Manage DSA IT control (-MM to make critical)\n"
86 " -n show what would be done but don't actually search\n"
87 " -O props SASL security properties\n"
88 " -p port port on LDAP server\n"
89 " -P version procotol version (default: 3)\n"
90 " -Q use SASL Quiet mode\n"
91 " -R realm SASL realm\n"
92 " -U authcid SASL authentication identity\n"
93 " -v run in verbose mode (diagnostics to standard output)\n"
94 " -w passwd bind passwd (for simple authentication)\n"
95 " -W prompt for bind passwd\n"
96 " -x Simple authentication\n"
97 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
98 " -y file Read passwd from file\n"
99 " -Y mech SASL mechanism\n"
100 " -Z Start TLS request (-ZZ to require successful response)\n"
101 , s, def_urlpre, def_tmpdir );
103 exit( EXIT_FAILURE );
106 static void print_entry LDAP_P((
111 static void print_reference(
113 LDAPMessage *reference );
115 static void print_extended(
117 LDAPMessage *extended );
119 static void print_partial(
121 LDAPMessage *partial );
123 static int print_result(
128 static void print_ctrls(
129 LDAPControl **ctrls );
131 static int write_ldif LDAP_P((
137 static int dosearch LDAP_P((
145 LDAPControl **sctrls,
146 LDAPControl **cctrls,
147 struct timeval *timeout,
150 static char *tmpdir = NULL;
151 static char *urlpre = NULL;
152 static char *prog = NULL;
153 static char *binddn = NULL;
154 static struct berval passwd = { 0, NULL };
155 static char *base = NULL;
156 static char *ldaphost = NULL;
157 static char *ldapuri = NULL;
158 static int ldapport = 0;
159 #ifdef HAVE_CYRUS_SASL
160 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
161 static char *sasl_realm = NULL;
162 static char *sasl_authc_id = NULL;
163 static char *sasl_authz_id = NULL;
164 static char *sasl_mech = NULL;
165 static char *sasl_secprops = NULL;
167 static int use_tls = 0;
168 static char *sortattr = NULL;
169 static int verbose, not, includeufn, vals2tmp, ldif;
176 if (*LDAP_DIRSEP != '/') {
177 for (p = url; *p; p++) {
178 if (*p == *LDAP_DIRSEP)
185 main( int argc, char **argv )
187 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
189 int rc, i, first, scope, deref, attrsonly, manageDSAit, noop, crit;
190 int referrals, timelimit, sizelimit, debug;
191 int authmethod, version, want_bindpw;
193 int valuesReturnFilter;
194 BerElement *ber = NULL;
195 struct berval *bvalp = NULL;
196 char *vrFilter = NULL, *control = NULL, *cvalue;
197 char *pw_file = NULL;
201 debug = verbose = not = vals2tmp = referrals = valuesReturnFilter =
202 attrsonly = manageDSAit = noop = ldif = want_bindpw = 0;
204 prog = lutil_progname( "ldapsearch", argc, argv );
206 lutil_log_initialize(argc, argv);
208 deref = sizelimit = timelimit = version = -1;
210 scope = LDAP_SCOPE_SUBTREE;
213 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
214 (def_tmpdir = getenv("TMP")) == NULL &&
215 (def_tmpdir = getenv("TEMP")) == NULL )
217 def_tmpdir = LDAP_TMPDIR;
221 def_tmpdir = LDAP_TMPDIR;
223 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
225 if( def_urlpre == NULL ) {
230 sprintf( def_urlpre, "file:///%s/",
231 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
233 urlize( def_urlpre );
235 while (( i = getopt( argc, argv, "Aa:b:E:F:f:Ll:S:s:T:tuz:"
236 "Cd:e:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z")) != EOF )
240 case 'a': /* set alias deref option */
241 if ( strcasecmp( optarg, "never" ) == 0 ) {
242 deref = LDAP_DEREF_NEVER;
243 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
244 deref = LDAP_DEREF_SEARCHING;
245 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
246 deref = LDAP_DEREF_FINDING;
247 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
248 deref = LDAP_DEREF_ALWAYS;
250 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
254 case 'A': /* retrieve attribute names only -- no values */
257 case 'b': /* search base */
258 base = strdup( optarg );
260 case 'E': /* search controls */
261 if( version == LDAP_VERSION2 ) {
262 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
267 /* should be extended to support comma separated list of
268 * [!]key[=value] parameters, e.g. -E !foo,bar=567
273 if( optarg[0] == '!' ) {
278 control = strdup( optarg );
279 if ( (cvalue = strchr( control, '=' )) != NULL ) {
283 if ( strcasecmp( control, "mv" ) == 0 ) {
284 /* ValuesReturnFilter control */
285 if (valuesReturnFilter!=0) {
286 fprintf( stderr, "ValuesReturnFilter previously specified");
289 valuesReturnFilter= 1 + crit;
291 if ( cvalue == NULL ) {
293 "missing filter in ValuesReturnFilter control\n");
298 version = LDAP_VERSION3;
302 fprintf( stderr, "Invalid control name: %s\n", control );
306 case 'f': /* input file */
307 if( infile != NULL ) {
308 fprintf( stderr, "%s: -f previously specified\n", prog );
311 infile = strdup( optarg );
313 case 'F': /* uri prefix */
314 if( urlpre ) free( urlpre );
315 urlpre = strdup( optarg );
317 case 'l': /* time limit */
318 timelimit = atoi( optarg );
319 if( timelimit < 0 ) {
320 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
325 case 'L': /* print entries in LDIF format */
328 case 's': /* search scope */
329 if ( strcasecmp( optarg, "base" ) == 0 ) {
330 scope = LDAP_SCOPE_BASE;
331 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
332 scope = LDAP_SCOPE_ONELEVEL;
333 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
334 scope = LDAP_SCOPE_SUBTREE;
336 fprintf( stderr, "scope should be base, one, or sub\n" );
340 case 'S': /* sort attribute */
341 sortattr = strdup( optarg );
343 case 'u': /* include UFN */
346 case 't': /* write attribute values to TMPDIR files */
349 case 'T': /* tmpdir */
350 if( tmpdir ) free( tmpdir );
351 tmpdir = strdup( optarg );
353 case 'z': /* size limit */
354 sizelimit = atoi( optarg );
362 debug |= atoi( optarg );
364 case 'D': /* bind DN */
365 if( binddn != NULL ) {
366 fprintf( stderr, "%s: -D previously specified\n", prog );
369 binddn = strdup( optarg );
371 case 'e': /* general controls */
372 if( version == LDAP_VERSION2 ) {
373 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
378 /* should be extended to support comma separated list of
379 * [!]key[=value] parameters, e.g. -e !foo,bar=567
384 if( optarg[0] == '!' ) {
389 control = strdup( optarg );
390 if ( (cvalue = strchr( control, '=' )) != NULL ) {
394 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
395 if( cvalue != NULL ) {
396 fprintf( stderr, "manageDSAit: no control value expected" );
401 manageDSAit = 1 + crit;
405 } else if ( strcasecmp( control, "noop" ) == 0 ) {
406 if( cvalue != NULL ) {
407 fprintf( stderr, "noop: no control value expected" );
417 fprintf( stderr, "Invalid general control name: %s\n", control );
421 case 'h': /* ldap host */
422 if( ldapuri != NULL ) {
423 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
426 if( ldaphost != NULL ) {
427 fprintf( stderr, "%s: -h previously specified\n", prog );
430 ldaphost = strdup( optarg );
432 case 'H': /* ldap URI */
433 if( ldaphost != NULL ) {
434 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
438 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
441 if( ldapuri != NULL ) {
442 fprintf( stderr, "%s: -H previously specified\n", prog );
445 ldapuri = strdup( optarg );
448 #ifdef HAVE_CYRUS_SASL
449 if( version == LDAP_VERSION2 ) {
450 fprintf( stderr, "%s: -I incompatible with version %d\n",
454 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
455 fprintf( stderr, "%s: incompatible previous "
456 "authentication choice\n",
460 authmethod = LDAP_AUTH_SASL;
461 version = LDAP_VERSION3;
462 sasl_flags = LDAP_SASL_INTERACTIVE;
465 fprintf( stderr, "%s: was not compiled with SASL support\n",
467 return( EXIT_FAILURE );
469 case 'k': /* kerberos bind */
470 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
471 if( version > LDAP_VERSION2 ) {
472 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
477 if( authmethod != -1 ) {
478 fprintf( stderr, "%s: -k incompatible with previous "
479 "authentication choice\n", prog );
483 authmethod = LDAP_AUTH_KRBV4;
485 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
489 case 'K': /* kerberos bind, part one only */
490 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
491 if( version > LDAP_VERSION2 ) {
492 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
496 if( authmethod != -1 ) {
497 fprintf( stderr, "%s: incompatible with previous "
498 "authentication choice\n", prog );
502 authmethod = LDAP_AUTH_KRBV41;
504 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
505 return( EXIT_FAILURE );
509 /* enable Manage DSA IT */
510 if( version == LDAP_VERSION2 ) {
511 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
516 version = LDAP_VERSION3;
518 case 'n': /* print deletes, don't actually do them */
522 #ifdef HAVE_CYRUS_SASL
523 if( sasl_secprops != NULL ) {
524 fprintf( stderr, "%s: -O previously specified\n", prog );
527 if( version == LDAP_VERSION2 ) {
528 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
532 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
533 fprintf( stderr, "%s: incompatible previous "
534 "authentication choice\n", prog );
537 authmethod = LDAP_AUTH_SASL;
538 version = LDAP_VERSION3;
539 sasl_secprops = strdup( optarg );
541 fprintf( stderr, "%s: not compiled with SASL support\n",
543 return( EXIT_FAILURE );
548 fprintf( stderr, "%s: -p previously specified\n", prog );
551 ldapport = atoi( optarg );
554 switch( atoi(optarg) ) {
556 if( version == LDAP_VERSION3 ) {
557 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
561 version = LDAP_VERSION2;
564 if( version == LDAP_VERSION2 ) {
565 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
569 version = LDAP_VERSION3;
572 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
575 return( EXIT_FAILURE );
578 #ifdef HAVE_CYRUS_SASL
579 if( version == LDAP_VERSION2 ) {
580 fprintf( stderr, "%s: -Q incompatible with version %d\n",
584 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
585 fprintf( stderr, "%s: incompatible previous "
586 "authentication choice\n",
590 authmethod = LDAP_AUTH_SASL;
591 version = LDAP_VERSION3;
592 sasl_flags = LDAP_SASL_QUIET;
595 fprintf( stderr, "%s: not compiled with SASL support\n",
597 return( EXIT_FAILURE );
600 #ifdef HAVE_CYRUS_SASL
601 if( sasl_realm != NULL ) {
602 fprintf( stderr, "%s: -R previously specified\n", prog );
605 if( version == LDAP_VERSION2 ) {
606 fprintf( stderr, "%s: -R incompatible with version %d\n",
610 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
611 fprintf( stderr, "%s: incompatible previous "
612 "authentication choice\n",
616 authmethod = LDAP_AUTH_SASL;
617 version = LDAP_VERSION3;
618 sasl_realm = strdup( optarg );
620 fprintf( stderr, "%s: not compiled with SASL support\n",
622 return( EXIT_FAILURE );
626 #ifdef HAVE_CYRUS_SASL
627 if( sasl_authc_id != NULL ) {
628 fprintf( stderr, "%s: -U previously specified\n", prog );
631 if( version == LDAP_VERSION2 ) {
632 fprintf( stderr, "%s: -U incompatible with version %d\n",
636 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
637 fprintf( stderr, "%s: incompatible previous "
638 "authentication choice\n",
642 authmethod = LDAP_AUTH_SASL;
643 version = LDAP_VERSION3;
644 sasl_authc_id = strdup( optarg );
646 fprintf( stderr, "%s: not compiled with SASL support\n",
648 return( EXIT_FAILURE );
651 case 'v': /* verbose mode */
654 case 'w': /* password */
655 passwd.bv_val = strdup( optarg );
659 for( p = optarg; *p != '\0'; p++ ) {
663 passwd.bv_len = strlen( passwd.bv_val );
672 #ifdef HAVE_CYRUS_SASL
673 if( sasl_mech != NULL ) {
674 fprintf( stderr, "%s: -Y previously specified\n", prog );
677 if( version == LDAP_VERSION2 ) {
678 fprintf( stderr, "%s: -Y incompatible with version %d\n",
682 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
683 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
686 authmethod = LDAP_AUTH_SASL;
687 version = LDAP_VERSION3;
688 sasl_mech = strdup( optarg );
690 fprintf( stderr, "%s: not compiled with SASL support\n",
692 return( EXIT_FAILURE );
696 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
697 fprintf( stderr, "%s: incompatible with previous "
698 "authentication choice\n", prog );
701 authmethod = LDAP_AUTH_SIMPLE;
704 #ifdef HAVE_CYRUS_SASL
705 if( sasl_authz_id != NULL ) {
706 fprintf( stderr, "%s: -X previously specified\n", prog );
709 if( version == LDAP_VERSION2 ) {
710 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
714 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
715 fprintf( stderr, "%s: -X incompatible with "
716 "authentication choice\n", prog );
719 authmethod = LDAP_AUTH_SASL;
720 version = LDAP_VERSION3;
721 sasl_authz_id = strdup( optarg );
723 fprintf( stderr, "%s: not compiled with SASL support\n",
725 return( EXIT_FAILURE );
730 if( version == LDAP_VERSION2 ) {
731 fprintf( stderr, "%s: -Z incompatible with version %d\n",
735 version = LDAP_VERSION3;
738 fprintf( stderr, "%s: not compiled with TLS support\n",
740 return( EXIT_FAILURE );
744 fprintf( stderr, "%s: unrecognized option -%c\n",
751 version = LDAP_VERSION3;
753 if (authmethod == -1 && version > LDAP_VERSION2) {
754 #ifdef HAVE_CYRUS_SASL
755 authmethod = LDAP_AUTH_SASL;
757 authmethod = LDAP_AUTH_SIMPLE;
761 if (( argc - optind < 1 ) ||
762 ( *argv[optind] != '(' /*')'*/ &&
763 ( strchr( argv[optind], '=' ) == NULL ) ) )
765 filtpattern = "(objectclass=*)";
767 filtpattern = strdup( argv[optind++] );
770 if ( argv[optind] != NULL ) {
771 attrs = &argv[optind];
774 if ( infile != NULL ) {
775 if ( infile[0] == '-' && infile[1] == '\0' ) {
777 } else if (( fp = fopen( infile, "r" )) == NULL ) {
783 if ( tmpdir == NULL ) {
786 if ( urlpre == NULL )
790 if( urlpre == NULL ) {
791 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
793 if( urlpre == NULL ) {
798 sprintf( urlpre, "file:///%s/",
799 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
805 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
806 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
808 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
809 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
815 (void) SIGNAL( SIGPIPE, SIG_IGN );
818 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
820 fprintf( stderr, "ldap_init( %s, %d )\n",
821 ldaphost != NULL ? ldaphost : "<DEFAULT>",
825 ld = ldap_init( ldaphost, ldapport );
827 perror("ldapsearch: ldap_init");
833 fprintf( stderr, "ldap_initialize( %s )\n",
834 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
837 rc = ldap_initialize( &ld, ldapuri );
838 if( rc != LDAP_SUCCESS ) {
839 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
840 rc, ldap_err2string(rc) );
846 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
848 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
851 if (timelimit != -1 &&
852 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
854 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
857 if (sizelimit != -1 &&
858 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
860 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
865 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
866 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
868 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
869 referrals ? "on" : "off" );
873 if (version == -1 ) {
874 version = LDAP_VERSION3;
877 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
878 != LDAP_OPT_SUCCESS )
880 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
885 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
886 ldap_perror( ld, "ldap_start_tls" );
892 if ( pw_file || want_bindpw ) {
894 rc = lutil_get_filed_password( pw_file, &passwd );
895 if( rc ) return EXIT_FAILURE;
897 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
898 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
902 if ( authmethod == LDAP_AUTH_SASL ) {
903 #ifdef HAVE_CYRUS_SASL
906 if( sasl_secprops != NULL ) {
907 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
908 (void *) sasl_secprops );
910 if( rc != LDAP_OPT_SUCCESS ) {
912 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
914 return( EXIT_FAILURE );
918 defaults = lutil_sasl_defaults( ld,
925 rc = ldap_sasl_interactive_bind_s( ld, binddn,
926 sasl_mech, NULL, NULL,
927 sasl_flags, lutil_sasl_interact, defaults );
929 if( rc != LDAP_SUCCESS ) {
930 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
931 return( EXIT_FAILURE );
934 fprintf( stderr, "%s: not compiled with SASL support\n",
936 return( EXIT_FAILURE );
939 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
941 ldap_perror( ld, "ldap_bind" );
942 return( EXIT_FAILURE );
946 if ( manageDSAit || valuesReturnFilter ) {
950 LDAPControl *ctrls[3];
956 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
957 c1.ldctl_value.bv_val = NULL;
958 c1.ldctl_value.bv_len = 0;
959 c1.ldctl_iscritical = manageDSAit > 1;
962 if ( valuesReturnFilter ) {
966 c2.ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
967 c2.ldctl_iscritical = valuesReturnFilter > 1;
969 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
973 if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
975 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
979 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
983 c2.ldctl_value=(*bvalp);
986 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
991 if( err != LDAP_OPT_SUCCESS ) {
992 fprintf( stderr, "Could not set %scontrols\n",
993 (c1.ldctl_iscritical || c2.ldctl_iscritical)
994 ? "critical " : "" );
995 if( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
1002 fprintf( stderr, "filter%s: %s\nrequesting: ",
1003 infile != NULL ? " pattern" : "",
1006 if ( attrs == NULL ) {
1007 fprintf( stderr, "ALL" );
1009 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1010 fprintf( stderr, "%s ", attrs[ i ] );
1013 fprintf( stderr, "\n" );
1017 printf( "# extended LDIF\n" );
1018 } else if ( ldif < 3 ) {
1019 printf( "version: %d\n\n", 1 );
1023 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
1025 infile != NULL ? " pattern" : "",
1028 if ( attrs == NULL ) {
1031 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1032 printf( "%s ", attrs[ i ] );
1036 if ( manageDSAit ) {
1037 printf("\n# with manageDSAit %scontrol",
1038 manageDSAit > 1 ? "critical " : "" );
1041 printf( "\n#\n\n" );
1044 if ( infile == NULL ) {
1045 rc = dosearch( ld, base, scope, NULL, filtpattern,
1046 attrs, attrsonly, NULL, NULL, NULL, -1 );
1051 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
1052 line[ strlen( line ) - 1 ] = '\0';
1058 rc = dosearch( ld, base, scope, filtpattern, line,
1059 attrs, attrsonly, NULL, NULL, NULL, -1 );
1061 if ( fp != stdin ) {
1071 static int dosearch(
1079 LDAPControl **sctrls,
1080 LDAPControl **cctrls,
1081 struct timeval *timeout,
1091 LDAPMessage *res, *msg;
1094 if( filtpatt != NULL ) {
1095 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1096 if( filter == NULL ) {
1098 return EXIT_FAILURE;
1101 sprintf( filter, filtpatt, value );
1104 fprintf( stderr, "filter: %s\n", filter );
1108 printf( "#\n# filter: %s\n#\n", filter );
1116 return LDAP_SUCCESS;
1119 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1120 sctrls, cctrls, timeout, sizelimit, &msgid );
1122 if ( filtpatt != NULL ) {
1126 if( rc != LDAP_SUCCESS ) {
1127 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1128 prog, ldap_err2string( rc ), rc );
1132 nresponses = nentries = nreferences = nextended = npartial = 0;
1136 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1137 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1141 (void) ldap_sort_entries( ld, &res,
1142 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1145 for ( msg = ldap_first_message( ld, res );
1147 msg = ldap_next_message( ld, msg ) )
1149 if( nresponses++ ) putchar('\n');
1151 switch( ldap_msgtype( msg ) ) {
1152 case LDAP_RES_SEARCH_ENTRY:
1154 print_entry( ld, msg, attrsonly );
1157 case LDAP_RES_SEARCH_REFERENCE:
1159 print_reference( ld, msg );
1162 case LDAP_RES_EXTENDED:
1164 print_extended( ld, msg );
1166 if( ldap_msgid( msg ) == 0 ) {
1167 /* unsolicited extended operation */
1172 case LDAP_RES_EXTENDED_PARTIAL:
1174 print_partial( ld, msg );
1177 case LDAP_RES_SEARCH_RESULT:
1178 rc = print_result( ld, msg, 1 );
1183 ldap_msgfree( res );
1187 ldap_perror( ld, "ldap_result" );
1193 printf( "\n# numResponses: %d\n", nresponses );
1194 if( nentries ) printf( "# numEntries: %d\n", nentries );
1195 if( nextended ) printf( "# numExtended: %d\n", nextended );
1196 if( npartial ) printf( "# numPartial: %d\n", npartial );
1197 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1210 char tmpfname[ 256 ];
1213 BerElement *ber = NULL;
1214 struct berval **bvals;
1215 LDAPControl **ctrls = NULL;
1218 dn = ldap_get_dn( ld, entry );
1222 ufn = ldap_dn2ufn( dn );
1223 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1225 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1227 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1229 if( rc != LDAP_SUCCESS ) {
1230 fprintf(stderr, "print_entry: %d\n", rc );
1231 ldap_perror( ld, "ldap_get_entry_controls" );
1232 exit( EXIT_FAILURE );
1236 print_ctrls( ctrls );
1237 ldap_controls_free( ctrls );
1242 ufn = ldap_dn2ufn( dn );
1244 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1247 if( ufn != NULL ) ldap_memfree( ufn );
1250 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1251 a = ldap_next_attribute( ld, entry, ber ) )
1254 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1256 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1257 for ( i = 0; bvals[i] != NULL; i++ ) {
1258 if ( vals2tmp > 1 || ( vals2tmp
1259 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1262 /* write value to file */
1263 snprintf( tmpfname, sizeof tmpfname,
1264 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1268 tmpfd = mkstemp( tmpfname );
1275 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1280 if ( fwrite( bvals[ i ]->bv_val,
1281 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1290 snprintf( url, sizeof url, "%s%s", urlpre,
1291 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1294 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1297 write_ldif( LDIF_PUT_VALUE, a,
1298 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1301 ber_bvecfree( bvals );
1310 static void print_reference(
1312 LDAPMessage *reference )
1316 LDAPControl **ctrls;
1319 printf("# search reference\n");
1322 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1324 if( rc != LDAP_SUCCESS ) {
1325 ldap_perror(ld, "ldap_parse_reference");
1326 exit( EXIT_FAILURE );
1331 for( i=0; refs[i] != NULL; i++ ) {
1332 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1333 "ref", refs[i], strlen(refs[i]) );
1335 ber_memvfree( (void **) refs );
1339 print_ctrls( ctrls );
1340 ldap_controls_free( ctrls );
1344 static void print_extended(
1346 LDAPMessage *extended )
1349 char *retoid = NULL;
1350 struct berval *retdata = NULL;
1353 printf("# extended result response\n");
1356 rc = ldap_parse_extended_result( ld, extended,
1357 &retoid, &retdata, 0 );
1359 if( rc != LDAP_SUCCESS ) {
1360 ldap_perror(ld, "ldap_parse_extended_result");
1361 exit( EXIT_FAILURE );
1364 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1365 "extended", retoid, retoid ? strlen(retoid) : 0 );
1366 ber_memfree( retoid );
1369 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1370 "data", retdata->bv_val, retdata->bv_len );
1371 ber_bvfree( retdata );
1374 print_result( ld, extended, 0 );
1377 static void print_partial(
1379 LDAPMessage *partial )
1382 char *retoid = NULL;
1383 struct berval *retdata = NULL;
1384 LDAPControl **ctrls = NULL;
1387 printf("# extended partial response\n");
1390 rc = ldap_parse_extended_partial( ld, partial,
1391 &retoid, &retdata, &ctrls, 0 );
1393 if( rc != LDAP_SUCCESS ) {
1394 ldap_perror(ld, "ldap_parse_extended_partial");
1395 exit( EXIT_FAILURE );
1398 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1399 "partial", retoid, retoid ? strlen(retoid) : 0 );
1401 ber_memfree( retoid );
1404 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1406 retdata->bv_val, retdata->bv_len );
1408 ber_bvfree( retdata );
1412 print_ctrls( ctrls );
1413 ldap_controls_free( ctrls );
1417 static int print_result(
1419 LDAPMessage *result, int search )
1423 char *matcheddn = NULL;
1426 LDAPControl **ctrls = NULL;
1430 printf("# search result\n");
1433 printf("%s: %d\n", "search", ldap_msgid(result) );
1437 rc = ldap_parse_result( ld, result,
1438 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1440 if( rc != LDAP_SUCCESS ) {
1441 ldap_perror(ld, "ldap_parse_result");
1442 exit( EXIT_FAILURE );
1447 printf( "result: %d %s\n", err, ldap_err2string(err) );
1449 } else if ( err != LDAP_SUCCESS ) {
1450 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1453 if( matcheddn && *matcheddn ) {
1455 write_ldif( LDIF_PUT_VALUE,
1456 "matchedDN", matcheddn, strlen(matcheddn) );
1458 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1461 ber_memfree( matcheddn );
1464 if( text && *text ) {
1466 write_ldif( LDIF_PUT_TEXT, "text",
1467 text, strlen(text) );
1469 fprintf( stderr, "Additional information: %s\n", text );
1472 ber_memfree( text );
1477 for( i=0; refs[i] != NULL; i++ ) {
1479 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1481 fprintf( stderr, "Referral: %s\n", refs[i] );
1485 ber_memvfree( (void **) refs );
1489 print_ctrls( ctrls );
1490 ldap_controls_free( ctrls );
1496 static void print_ctrls(
1497 LDAPControl **ctrls )
1500 for(i=0; ctrls[i] != NULL; i++ ) {
1501 /* control: OID criticality base64value */
1502 struct berval *b64 = NULL;
1506 len = strlen( ctrls[i]->ldctl_oid );
1508 /* add enough for space after OID and the critical value itself */
1509 len += ctrls[i]->ldctl_iscritical
1510 ? sizeof("true") : sizeof("false");
1512 /* convert to base64 */
1513 if( ctrls[i]->ldctl_value.bv_len ) {
1514 b64 = ber_memalloc( sizeof(struct berval) );
1516 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1517 ctrls[i]->ldctl_value.bv_len ) + 1;
1518 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1520 b64->bv_len = lutil_b64_ntop(
1521 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1522 b64->bv_val, b64->bv_len );
1526 len += 1 + b64->bv_len;
1529 str = malloc( len + 1 );
1530 strcpy( str, ctrls[i]->ldctl_oid );
1531 strcat( str, ctrls[i]->ldctl_iscritical
1532 ? " true" : " false" );
1536 strcat(str, b64->bv_val );
1539 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1540 "control", str, len );
1548 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1552 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1556 fputs( ldif, stdout );
1557 ber_memfree( ldif );