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\n" );
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\n" );
401 manageDSAit = 1 + crit;
405 } else if ( strcasecmp( control, "noop" ) == 0 ) {
406 if( cvalue != NULL ) {
407 fprintf( stderr, "noop: no control value expected\n" );
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 || noop || valuesReturnFilter ) {
949 LDAPControl c1,c2,c3;
950 LDAPControl *ctrls[4];
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;
966 c2.ldctl_oid = LDAP_CONTROL_NOOP;
967 c2.ldctl_value.bv_val = NULL;
968 c2.ldctl_value.bv_len = 0;
969 c2.ldctl_iscritical = noop > 1;
972 if ( valuesReturnFilter ) {
976 c3.ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
977 c3.ldctl_iscritical = valuesReturnFilter > 1;
979 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
983 if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
985 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
989 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
993 c3.ldctl_value=(*bvalp);
996 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1001 if( err != LDAP_OPT_SUCCESS ) {
1002 fprintf( stderr, "Could not set %scontrols\n",
1003 (c1.ldctl_iscritical || c2.ldctl_iscritical)
1004 ? "critical " : "" );
1005 if( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
1006 return EXIT_FAILURE;
1012 fprintf( stderr, "filter%s: %s\nrequesting: ",
1013 infile != NULL ? " pattern" : "",
1016 if ( attrs == NULL ) {
1017 fprintf( stderr, "ALL" );
1019 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1020 fprintf( stderr, "%s ", attrs[ i ] );
1023 fprintf( stderr, "\n" );
1027 printf( "# extended LDIF\n" );
1028 } else if ( ldif < 3 ) {
1029 printf( "version: %d\n\n", 1 );
1035 "# base <%s> with scope %s\n"
1039 base, (scope == LDAP_SCOPE_BASE) ? "base"
1040 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"),
1041 infile != NULL ? " pattern" : "",
1044 if ( attrs == NULL ) {
1047 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1048 printf( "%s ", attrs[ i ] );
1052 if ( manageDSAit ) {
1053 printf("\n# with manageDSAit %scontrol",
1054 manageDSAit > 1 ? "critical " : "" );
1057 printf("\n# with noop %scontrol",
1058 noop > 1 ? "critical " : "" );
1060 if ( valuesReturnFilter ) {
1061 printf("\n# with valuesReturnFilter %scontrol: %s",
1062 valuesReturnFilter > 1 ? "critical " : "", vrFilter );
1065 printf( "\n#\n\n" );
1068 if ( infile == NULL ) {
1069 rc = dosearch( ld, base, scope, NULL, filtpattern,
1070 attrs, attrsonly, NULL, NULL, NULL, -1 );
1075 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
1076 line[ strlen( line ) - 1 ] = '\0';
1082 rc = dosearch( ld, base, scope, filtpattern, line,
1083 attrs, attrsonly, NULL, NULL, NULL, -1 );
1085 if ( fp != stdin ) {
1095 static int dosearch(
1103 LDAPControl **sctrls,
1104 LDAPControl **cctrls,
1105 struct timeval *timeout,
1115 LDAPMessage *res, *msg;
1118 if( filtpatt != NULL ) {
1119 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1120 if( filter == NULL ) {
1122 return EXIT_FAILURE;
1125 sprintf( filter, filtpatt, value );
1128 fprintf( stderr, "filter: %s\n", filter );
1132 printf( "#\n# filter: %s\n#\n", filter );
1140 return LDAP_SUCCESS;
1143 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1144 sctrls, cctrls, timeout, sizelimit, &msgid );
1146 if ( filtpatt != NULL ) {
1150 if( rc != LDAP_SUCCESS ) {
1151 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1152 prog, ldap_err2string( rc ), rc );
1156 nresponses = nentries = nreferences = nextended = npartial = 0;
1160 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1161 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1165 (void) ldap_sort_entries( ld, &res,
1166 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1169 for ( msg = ldap_first_message( ld, res );
1171 msg = ldap_next_message( ld, msg ) )
1173 if( nresponses++ ) putchar('\n');
1175 switch( ldap_msgtype( msg ) ) {
1176 case LDAP_RES_SEARCH_ENTRY:
1178 print_entry( ld, msg, attrsonly );
1181 case LDAP_RES_SEARCH_REFERENCE:
1183 print_reference( ld, msg );
1186 case LDAP_RES_EXTENDED:
1188 print_extended( ld, msg );
1190 if( ldap_msgid( msg ) == 0 ) {
1191 /* unsolicited extended operation */
1196 case LDAP_RES_EXTENDED_PARTIAL:
1198 print_partial( ld, msg );
1201 case LDAP_RES_SEARCH_RESULT:
1202 rc = print_result( ld, msg, 1 );
1207 ldap_msgfree( res );
1211 ldap_perror( ld, "ldap_result" );
1217 printf( "\n# numResponses: %d\n", nresponses );
1218 if( nentries ) printf( "# numEntries: %d\n", nentries );
1219 if( nextended ) printf( "# numExtended: %d\n", nextended );
1220 if( npartial ) printf( "# numPartial: %d\n", npartial );
1221 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1228 /* This is the original version, the old way of doing things. */
1236 char tmpfname[ 256 ];
1239 BerElement *ber = NULL;
1240 struct berval **bvals;
1241 LDAPControl **ctrls = NULL;
1244 dn = ldap_get_dn( ld, entry );
1248 ufn = ldap_dn2ufn( dn );
1249 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1251 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1253 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1255 if( rc != LDAP_SUCCESS ) {
1256 fprintf(stderr, "print_entry: %d\n", rc );
1257 ldap_perror( ld, "ldap_get_entry_controls" );
1258 exit( EXIT_FAILURE );
1262 print_ctrls( ctrls );
1263 ldap_controls_free( ctrls );
1268 ufn = ldap_dn2ufn( dn );
1270 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1273 if( ufn != NULL ) ldap_memfree( ufn );
1276 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1277 a = ldap_next_attribute( ld, entry, ber ) )
1280 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1282 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1283 for ( i = 0; bvals[i] != NULL; i++ ) {
1284 if ( vals2tmp > 1 || ( vals2tmp
1285 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1288 /* write value to file */
1289 snprintf( tmpfname, sizeof tmpfname,
1290 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1294 tmpfd = mkstemp( tmpfname );
1301 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1306 if ( fwrite( bvals[ i ]->bv_val,
1307 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1316 snprintf( url, sizeof url, "%s%s", urlpre,
1317 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1320 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1323 write_ldif( LDIF_PUT_VALUE, a,
1324 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1327 ber_bvecfree( bvals );
1336 /* This is the proposed new way of doing things.
1337 * It is more * efficient, but the API is non-standard.
1346 char tmpfname[ 256 ];
1349 BerElement *ber = NULL;
1350 struct berval bv, *bvals, **bvp = &bvals;
1351 LDAPControl **ctrls = NULL;
1354 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1357 ufn = ldap_dn2ufn( bv.bv_val );
1358 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1360 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1362 rc = ldap_int_get_controls( ber, &ctrls );
1364 if( rc != LDAP_SUCCESS ) {
1365 fprintf(stderr, "print_entry: %d\n", rc );
1366 ldap_perror( ld, "ldap_get_entry_controls" );
1367 exit( EXIT_FAILURE );
1371 print_ctrls( ctrls );
1372 ldap_controls_free( ctrls );
1377 ufn = ldap_dn2ufn( bv.bv_val );
1379 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1382 if( ufn != NULL ) ldap_memfree( ufn );
1384 if ( attrsonly ) bvp = NULL;
1386 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ); rc == LDAP_SUCCESS;
1387 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1389 if (bv.bv_val == NULL) break;
1392 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1395 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1396 if ( vals2tmp > 1 || ( vals2tmp
1397 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1400 /* write value to file */
1401 snprintf( tmpfname, sizeof tmpfname,
1402 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1403 tmpdir, bv.bv_val );
1406 tmpfd = mkstemp( tmpfname );
1413 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1418 if ( fwrite( bvals[ i ].bv_val,
1419 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1428 snprintf( url, sizeof url, "%s%s", urlpre,
1429 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1432 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1435 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1436 bvals[ i ].bv_val, bvals[ i ].bv_len );
1439 ber_memfree( bvals );
1449 static void print_reference(
1451 LDAPMessage *reference )
1455 LDAPControl **ctrls;
1458 printf("# search reference\n");
1461 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1463 if( rc != LDAP_SUCCESS ) {
1464 ldap_perror(ld, "ldap_parse_reference");
1465 exit( EXIT_FAILURE );
1470 for( i=0; refs[i] != NULL; i++ ) {
1471 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1472 "ref", refs[i], strlen(refs[i]) );
1474 ber_memvfree( (void **) refs );
1478 print_ctrls( ctrls );
1479 ldap_controls_free( ctrls );
1483 static void print_extended(
1485 LDAPMessage *extended )
1488 char *retoid = NULL;
1489 struct berval *retdata = NULL;
1492 printf("# extended result response\n");
1495 rc = ldap_parse_extended_result( ld, extended,
1496 &retoid, &retdata, 0 );
1498 if( rc != LDAP_SUCCESS ) {
1499 ldap_perror(ld, "ldap_parse_extended_result");
1500 exit( EXIT_FAILURE );
1504 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1505 "extended", retoid, retoid ? strlen(retoid) : 0 );
1507 ber_memfree( retoid );
1511 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1512 "data", retdata->bv_val, retdata->bv_len );
1514 ber_bvfree( retdata );
1517 print_result( ld, extended, 0 );
1520 static void print_partial(
1522 LDAPMessage *partial )
1525 char *retoid = NULL;
1526 struct berval *retdata = NULL;
1527 LDAPControl **ctrls = NULL;
1530 printf("# extended partial response\n");
1533 rc = ldap_parse_extended_partial( ld, partial,
1534 &retoid, &retdata, &ctrls, 0 );
1536 if( rc != LDAP_SUCCESS ) {
1537 ldap_perror(ld, "ldap_parse_extended_partial");
1538 exit( EXIT_FAILURE );
1542 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1543 "partial", retoid, retoid ? strlen(retoid) : 0 );
1546 ber_memfree( retoid );
1550 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1551 "data", retdata->bv_val, retdata->bv_len );
1554 ber_bvfree( retdata );
1558 print_ctrls( ctrls );
1559 ldap_controls_free( ctrls );
1563 static int print_result(
1565 LDAPMessage *result, int search )
1569 char *matcheddn = NULL;
1572 LDAPControl **ctrls = NULL;
1576 printf("# search result\n");
1579 printf("%s: %d\n", "search", ldap_msgid(result) );
1583 rc = ldap_parse_result( ld, result,
1584 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1586 if( rc != LDAP_SUCCESS ) {
1587 ldap_perror(ld, "ldap_parse_result");
1588 exit( EXIT_FAILURE );
1593 printf( "result: %d %s\n", err, ldap_err2string(err) );
1595 } else if ( err != LDAP_SUCCESS ) {
1596 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1599 if( matcheddn && *matcheddn ) {
1601 write_ldif( LDIF_PUT_VALUE,
1602 "matchedDN", matcheddn, strlen(matcheddn) );
1604 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1607 ber_memfree( matcheddn );
1610 if( text && *text ) {
1612 write_ldif( LDIF_PUT_TEXT, "text",
1613 text, strlen(text) );
1615 fprintf( stderr, "Additional information: %s\n", text );
1618 ber_memfree( text );
1623 for( i=0; refs[i] != NULL; i++ ) {
1625 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1627 fprintf( stderr, "Referral: %s\n", refs[i] );
1631 ber_memvfree( (void **) refs );
1635 print_ctrls( ctrls );
1636 ldap_controls_free( ctrls );
1642 static void print_ctrls(
1643 LDAPControl **ctrls )
1646 for(i=0; ctrls[i] != NULL; i++ ) {
1647 /* control: OID criticality base64value */
1648 struct berval *b64 = NULL;
1653 len += strlen( ctrls[i]->ldctl_oid );
1655 /* add enough for space after OID and the critical value itself */
1656 len += ctrls[i]->ldctl_iscritical
1657 ? sizeof("true") : sizeof("false");
1659 /* convert to base64 */
1660 if( ctrls[i]->ldctl_value.bv_len ) {
1661 b64 = ber_memalloc( sizeof(struct berval) );
1663 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1664 ctrls[i]->ldctl_value.bv_len ) + 1;
1665 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1667 b64->bv_len = lutil_b64_ntop(
1668 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1669 b64->bv_val, b64->bv_len );
1673 len += 1 + b64->bv_len;
1676 str = malloc( len + 1 );
1678 strcpy( str, ": " );
1682 strcat( str, ctrls[i]->ldctl_oid );
1683 strcat( str, ctrls[i]->ldctl_iscritical
1684 ? " true" : " false" );
1688 strcat(str, b64->bv_val );
1692 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1693 "control", str, len );
1702 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1706 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1710 fputs( ldif, stdout );
1711 ber_memfree( ldif );