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 #ifdef LDAP_CONTROL_SUBENTRIES
60 " [!]subentries[=true|false] (subentries)\n"
62 " -F prefix URL prefix for files (default: %s)\n"
63 " -l limit time limit (in seconds) for search\n"
64 " -L print responses in LDIFv1 format\n"
65 " -LL print responses in LDIF format without comments\n"
66 " -LLL print responses in LDIF format without comments\n"
68 " -s scope one of base, one, or sub (search scope)\n"
69 " -S attr sort the results by attribute `attr'\n"
70 " -t write binary values to files in temporary directory\n"
71 " -tt write all values to files in temporary directory\n"
72 " -T path write files to directory specified by path (default: %s)\n"
73 " -u include User Friendly entry names in the output\n"
74 " -z limit size limit (in entries) for search\n"
77 " -d level set LDAP debugging level to `level'\n"
78 " -D binddn bind DN\n"
79 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
80 " [!]manageDSAit (alternate form, see -M)\n"
82 " -f file read operations from `file'\n"
83 " -h host LDAP server\n"
84 " -H URI LDAP Uniform Resource Indentifier(s)\n"
85 " -I use SASL Interactive mode\n"
86 " -k use Kerberos authentication\n"
87 " -K like -k, but do only step 1 of the Kerberos bind\n"
88 " -M enable Manage DSA IT control (-MM to make critical)\n"
89 " -n show what would be done but don't actually search\n"
90 " -O props SASL security properties\n"
91 " -p port port on LDAP server\n"
92 " -P version procotol version (default: 3)\n"
93 " -Q use SASL Quiet mode\n"
94 " -R realm SASL realm\n"
95 " -U authcid SASL authentication identity\n"
96 " -v run in verbose mode (diagnostics to standard output)\n"
97 " -w passwd bind passwd (for simple authentication)\n"
98 " -W prompt for bind passwd\n"
99 " -x Simple authentication\n"
100 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
101 " -y file Read passwd from file\n"
102 " -Y mech SASL mechanism\n"
103 " -Z Start TLS request (-ZZ to require successful response)\n"
104 , s, def_urlpre, def_tmpdir );
106 exit( EXIT_FAILURE );
109 static void print_entry LDAP_P((
114 static void print_reference(
116 LDAPMessage *reference );
118 static void print_extended(
120 LDAPMessage *extended );
122 static void print_partial(
124 LDAPMessage *partial );
126 static int print_result(
131 static void print_ctrls(
132 LDAPControl **ctrls );
134 static int write_ldif LDAP_P((
140 static int dosearch LDAP_P((
148 LDAPControl **sctrls,
149 LDAPControl **cctrls,
150 struct timeval *timeout,
153 static char *tmpdir = NULL;
154 static char *urlpre = NULL;
155 static char *prog = NULL;
156 static char *binddn = NULL;
157 static struct berval passwd = { 0, NULL };
158 static char *base = NULL;
159 static char *ldaphost = NULL;
160 static char *ldapuri = NULL;
161 static int ldapport = 0;
162 #ifdef HAVE_CYRUS_SASL
163 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
164 static char *sasl_realm = NULL;
165 static char *sasl_authc_id = NULL;
166 static char *sasl_authz_id = NULL;
167 static char *sasl_mech = NULL;
168 static char *sasl_secprops = NULL;
170 static int use_tls = 0;
171 static char *sortattr = NULL;
172 static int verbose, not, includeufn, vals2tmp, ldif;
179 if (*LDAP_DIRSEP != '/') {
180 for (p = url; *p; p++) {
181 if (*p == *LDAP_DIRSEP)
188 main( int argc, char **argv )
190 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
192 int rc, i, first, scope, deref, attrsonly, manageDSAit, noop, crit;
193 int referrals, timelimit, sizelimit, debug;
194 int authmethod, version, want_bindpw;
196 int subentries, valuesReturnFilter;
197 BerElement *ber = NULL;
198 struct berval *bvalp = NULL;
199 char *vrFilter = NULL, *control = NULL, *cvalue;
200 char *pw_file = NULL;
204 debug = verbose = not = vals2tmp = referrals =
205 subentries = valuesReturnFilter =
206 attrsonly = manageDSAit = noop = ldif = want_bindpw = 0;
208 prog = lutil_progname( "ldapsearch", argc, argv );
210 lutil_log_initialize(argc, argv);
212 deref = sizelimit = timelimit = version = -1;
214 scope = LDAP_SCOPE_SUBTREE;
217 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
218 (def_tmpdir = getenv("TMP")) == NULL &&
219 (def_tmpdir = getenv("TEMP")) == NULL )
221 def_tmpdir = LDAP_TMPDIR;
225 def_tmpdir = LDAP_TMPDIR;
227 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
229 if( def_urlpre == NULL ) {
234 sprintf( def_urlpre, "file:///%s/",
235 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
237 urlize( def_urlpre );
239 while (( i = getopt( argc, argv, "Aa:b:E:F:f:Ll:S:s:T:tuz:"
240 "Cd:e:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z")) != EOF )
244 case 'a': /* set alias deref option */
245 if ( strcasecmp( optarg, "never" ) == 0 ) {
246 deref = LDAP_DEREF_NEVER;
247 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
248 deref = LDAP_DEREF_SEARCHING;
249 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
250 deref = LDAP_DEREF_FINDING;
251 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
252 deref = LDAP_DEREF_ALWAYS;
254 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
258 case 'A': /* retrieve attribute names only -- no values */
261 case 'b': /* search base */
262 base = strdup( optarg );
264 case 'E': /* search controls */
265 if( version == LDAP_VERSION2 ) {
266 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
271 /* should be extended to support comma separated list of
272 * [!]key[=value] parameters, e.g. -E !foo,bar=567
277 if( optarg[0] == '!' ) {
282 control = strdup( optarg );
283 if ( (cvalue = strchr( control, '=' )) != NULL ) {
287 if ( strcasecmp( control, "mv" ) == 0 ) {
288 /* ValuesReturnFilter control */
289 if( valuesReturnFilter ) {
290 fprintf( stderr, "ValuesReturnFilter previously specified");
293 valuesReturnFilter= 1 + crit;
295 if ( cvalue == NULL ) {
297 "missing filter in ValuesReturnFilter control\n");
302 version = LDAP_VERSION3;
305 #ifdef LDAP_CONTROL_SUBENTRIES
306 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
308 fprintf( stderr, "subentries control previously specified");
311 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
313 } else if ( strcasecmp( cvalue, "false") == 0 ) {
317 "subentries control value \"%s\" invalid\n");
320 if( crit ) subentries *= -1;
324 fprintf( stderr, "Invalid control name: %s\n", control );
328 case 'f': /* input file */
329 if( infile != NULL ) {
330 fprintf( stderr, "%s: -f previously specified\n", prog );
333 infile = strdup( optarg );
335 case 'F': /* uri prefix */
336 if( urlpre ) free( urlpre );
337 urlpre = strdup( optarg );
339 case 'l': /* time limit */
340 timelimit = atoi( optarg );
341 if( timelimit < 0 ) {
342 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
347 case 'L': /* print entries in LDIF format */
350 case 's': /* search scope */
351 if ( strcasecmp( optarg, "base" ) == 0 ) {
352 scope = LDAP_SCOPE_BASE;
353 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
354 scope = LDAP_SCOPE_ONELEVEL;
355 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
356 scope = LDAP_SCOPE_SUBTREE;
358 fprintf( stderr, "scope should be base, one, or sub\n" );
362 case 'S': /* sort attribute */
363 sortattr = strdup( optarg );
365 case 'u': /* include UFN */
368 case 't': /* write attribute values to TMPDIR files */
371 case 'T': /* tmpdir */
372 if( tmpdir ) free( tmpdir );
373 tmpdir = strdup( optarg );
375 case 'z': /* size limit */
376 sizelimit = atoi( optarg );
384 debug |= atoi( optarg );
386 case 'D': /* bind DN */
387 if( binddn != NULL ) {
388 fprintf( stderr, "%s: -D previously specified\n", prog );
391 binddn = strdup( optarg );
393 case 'e': /* general controls */
394 if( version == LDAP_VERSION2 ) {
395 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
400 /* should be extended to support comma separated list of
401 * [!]key[=value] parameters, e.g. -e !foo,bar=567
406 if( optarg[0] == '!' ) {
411 control = strdup( optarg );
412 if ( (cvalue = strchr( control, '=' )) != NULL ) {
416 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
418 fprintf( stderr, "manageDSAit control previously specified");
421 if( cvalue != NULL ) {
422 fprintf( stderr, "manageDSAit: no control value expected" );
427 manageDSAit = 1 + crit;
431 } else if ( strcasecmp( control, "noop" ) == 0 ) {
433 fprintf( stderr, "noop control previously specified");
436 if( cvalue != NULL ) {
437 fprintf( stderr, "noop: no control value expected" );
447 fprintf( stderr, "Invalid general control name: %s\n", control );
451 case 'h': /* ldap host */
452 if( ldapuri != NULL ) {
453 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
456 if( ldaphost != NULL ) {
457 fprintf( stderr, "%s: -h previously specified\n", prog );
460 ldaphost = strdup( optarg );
462 case 'H': /* ldap URI */
463 if( ldaphost != NULL ) {
464 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
468 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
471 if( ldapuri != NULL ) {
472 fprintf( stderr, "%s: -H previously specified\n", prog );
475 ldapuri = strdup( optarg );
478 #ifdef HAVE_CYRUS_SASL
479 if( version == LDAP_VERSION2 ) {
480 fprintf( stderr, "%s: -I incompatible with version %d\n",
484 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
485 fprintf( stderr, "%s: incompatible previous "
486 "authentication choice\n",
490 authmethod = LDAP_AUTH_SASL;
491 version = LDAP_VERSION3;
492 sasl_flags = LDAP_SASL_INTERACTIVE;
495 fprintf( stderr, "%s: was not compiled with SASL support\n",
497 return( EXIT_FAILURE );
499 case 'k': /* kerberos bind */
500 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
501 if( version > LDAP_VERSION2 ) {
502 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
507 if( authmethod != -1 ) {
508 fprintf( stderr, "%s: -k incompatible with previous "
509 "authentication choice\n", prog );
513 authmethod = LDAP_AUTH_KRBV4;
515 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
519 case 'K': /* kerberos bind, part one only */
520 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
521 if( version > LDAP_VERSION2 ) {
522 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
526 if( authmethod != -1 ) {
527 fprintf( stderr, "%s: incompatible with previous "
528 "authentication choice\n", prog );
532 authmethod = LDAP_AUTH_KRBV41;
534 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
535 return( EXIT_FAILURE );
539 /* enable Manage DSA IT */
540 if( version == LDAP_VERSION2 ) {
541 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
546 version = LDAP_VERSION3;
548 case 'n': /* print deletes, don't actually do them */
552 #ifdef HAVE_CYRUS_SASL
553 if( sasl_secprops != NULL ) {
554 fprintf( stderr, "%s: -O previously specified\n", prog );
557 if( version == LDAP_VERSION2 ) {
558 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
562 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
563 fprintf( stderr, "%s: incompatible previous "
564 "authentication choice\n", prog );
567 authmethod = LDAP_AUTH_SASL;
568 version = LDAP_VERSION3;
569 sasl_secprops = strdup( optarg );
571 fprintf( stderr, "%s: not compiled with SASL support\n",
573 return( EXIT_FAILURE );
578 fprintf( stderr, "%s: -p previously specified\n", prog );
581 ldapport = atoi( optarg );
584 switch( atoi(optarg) ) {
586 if( version == LDAP_VERSION3 ) {
587 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
591 version = LDAP_VERSION2;
594 if( version == LDAP_VERSION2 ) {
595 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
599 version = LDAP_VERSION3;
602 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
605 return( EXIT_FAILURE );
608 #ifdef HAVE_CYRUS_SASL
609 if( version == LDAP_VERSION2 ) {
610 fprintf( stderr, "%s: -Q incompatible with version %d\n",
614 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
615 fprintf( stderr, "%s: incompatible previous "
616 "authentication choice\n",
620 authmethod = LDAP_AUTH_SASL;
621 version = LDAP_VERSION3;
622 sasl_flags = LDAP_SASL_QUIET;
625 fprintf( stderr, "%s: not compiled with SASL support\n",
627 return( EXIT_FAILURE );
630 #ifdef HAVE_CYRUS_SASL
631 if( sasl_realm != NULL ) {
632 fprintf( stderr, "%s: -R previously specified\n", prog );
635 if( version == LDAP_VERSION2 ) {
636 fprintf( stderr, "%s: -R incompatible with version %d\n",
640 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
641 fprintf( stderr, "%s: incompatible previous "
642 "authentication choice\n",
646 authmethod = LDAP_AUTH_SASL;
647 version = LDAP_VERSION3;
648 sasl_realm = strdup( optarg );
650 fprintf( stderr, "%s: not compiled with SASL support\n",
652 return( EXIT_FAILURE );
656 #ifdef HAVE_CYRUS_SASL
657 if( sasl_authc_id != NULL ) {
658 fprintf( stderr, "%s: -U previously specified\n", prog );
661 if( version == LDAP_VERSION2 ) {
662 fprintf( stderr, "%s: -U incompatible with version %d\n",
666 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
667 fprintf( stderr, "%s: incompatible previous "
668 "authentication choice\n",
672 authmethod = LDAP_AUTH_SASL;
673 version = LDAP_VERSION3;
674 sasl_authc_id = strdup( optarg );
676 fprintf( stderr, "%s: not compiled with SASL support\n",
678 return( EXIT_FAILURE );
681 case 'v': /* verbose mode */
684 case 'w': /* password */
685 passwd.bv_val = strdup( optarg );
689 for( p = optarg; *p != '\0'; p++ ) {
693 passwd.bv_len = strlen( passwd.bv_val );
702 #ifdef HAVE_CYRUS_SASL
703 if( sasl_mech != NULL ) {
704 fprintf( stderr, "%s: -Y previously specified\n", prog );
707 if( version == LDAP_VERSION2 ) {
708 fprintf( stderr, "%s: -Y incompatible with version %d\n",
712 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
713 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
716 authmethod = LDAP_AUTH_SASL;
717 version = LDAP_VERSION3;
718 sasl_mech = strdup( optarg );
720 fprintf( stderr, "%s: not compiled with SASL support\n",
722 return( EXIT_FAILURE );
726 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
727 fprintf( stderr, "%s: incompatible with previous "
728 "authentication choice\n", prog );
731 authmethod = LDAP_AUTH_SIMPLE;
734 #ifdef HAVE_CYRUS_SASL
735 if( sasl_authz_id != NULL ) {
736 fprintf( stderr, "%s: -X previously specified\n", prog );
739 if( version == LDAP_VERSION2 ) {
740 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
744 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
745 fprintf( stderr, "%s: -X incompatible with "
746 "authentication choice\n", prog );
749 authmethod = LDAP_AUTH_SASL;
750 version = LDAP_VERSION3;
751 sasl_authz_id = strdup( optarg );
753 fprintf( stderr, "%s: not compiled with SASL support\n",
755 return( EXIT_FAILURE );
760 if( version == LDAP_VERSION2 ) {
761 fprintf( stderr, "%s: -Z incompatible with version %d\n",
765 version = LDAP_VERSION3;
768 fprintf( stderr, "%s: not compiled with TLS support\n",
770 return( EXIT_FAILURE );
774 fprintf( stderr, "%s: unrecognized option -%c\n",
781 version = LDAP_VERSION3;
783 if (authmethod == -1 && version > LDAP_VERSION2) {
784 #ifdef HAVE_CYRUS_SASL
785 authmethod = LDAP_AUTH_SASL;
787 authmethod = LDAP_AUTH_SIMPLE;
791 if (( argc - optind < 1 ) ||
792 ( *argv[optind] != '(' /*')'*/ &&
793 ( strchr( argv[optind], '=' ) == NULL ) ) )
795 filtpattern = "(objectclass=*)";
797 filtpattern = strdup( argv[optind++] );
800 if ( argv[optind] != NULL ) {
801 attrs = &argv[optind];
804 if ( infile != NULL ) {
805 if ( infile[0] == '-' && infile[1] == '\0' ) {
807 } else if (( fp = fopen( infile, "r" )) == NULL ) {
813 if ( tmpdir == NULL ) {
816 if ( urlpre == NULL )
820 if( urlpre == NULL ) {
821 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
823 if( urlpre == NULL ) {
828 sprintf( urlpre, "file:///%s/",
829 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
835 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
836 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
838 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
839 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
845 (void) SIGNAL( SIGPIPE, SIG_IGN );
848 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
850 fprintf( stderr, "ldap_init( %s, %d )\n",
851 ldaphost != NULL ? ldaphost : "<DEFAULT>",
855 ld = ldap_init( ldaphost, ldapport );
857 perror("ldapsearch: ldap_init");
863 fprintf( stderr, "ldap_initialize( %s )\n",
864 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
867 rc = ldap_initialize( &ld, ldapuri );
868 if( rc != LDAP_SUCCESS ) {
869 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
870 rc, ldap_err2string(rc) );
876 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
878 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
881 if (timelimit != -1 &&
882 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
884 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
887 if (sizelimit != -1 &&
888 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
890 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
895 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
896 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
898 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
899 referrals ? "on" : "off" );
903 if (version == -1 ) {
904 version = LDAP_VERSION3;
907 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
908 != LDAP_OPT_SUCCESS )
910 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
915 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
916 ldap_perror( ld, "ldap_start_tls" );
922 if ( pw_file || want_bindpw ) {
924 rc = lutil_get_filed_password( pw_file, &passwd );
925 if( rc ) return EXIT_FAILURE;
927 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
928 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
932 if ( authmethod == LDAP_AUTH_SASL ) {
933 #ifdef HAVE_CYRUS_SASL
936 if( sasl_secprops != NULL ) {
937 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
938 (void *) sasl_secprops );
940 if( rc != LDAP_OPT_SUCCESS ) {
942 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
944 return( EXIT_FAILURE );
948 defaults = lutil_sasl_defaults( ld,
955 rc = ldap_sasl_interactive_bind_s( ld, binddn,
956 sasl_mech, NULL, NULL,
957 sasl_flags, lutil_sasl_interact, defaults );
959 if( rc != LDAP_SUCCESS ) {
960 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
961 return( EXIT_FAILURE );
964 fprintf( stderr, "%s: not compiled with SASL support\n",
966 return( EXIT_FAILURE );
969 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
971 ldap_perror( ld, "ldap_bind" );
972 return( EXIT_FAILURE );
976 if ( manageDSAit || noop || valuesReturnFilter ) {
979 LDAPControl c1,c2,c3,c4;
980 LDAPControl *ctrls[5];
986 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
987 c1.ldctl_value.bv_val = NULL;
988 c1.ldctl_value.bv_len = 0;
989 c1.ldctl_iscritical = manageDSAit > 1;
996 c2.ldctl_oid = LDAP_CONTROL_NOOP;
997 c2.ldctl_value.bv_val = NULL;
998 c2.ldctl_value.bv_len = 0;
999 c2.ldctl_iscritical = noop > 1;
1002 #ifdef LDAP_CONTROL_SUBENTRIES
1007 c3.ldctl_oid = LDAP_CONTROL_SUBENTRIES;
1008 c3.ldctl_iscritical = subentries < 1;
1010 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1011 return EXIT_FAILURE;
1014 err = ber_printf( ber, "{b}", abs(subentries) == 1 ? 0 : 1 );
1015 if ( err == LBER_ERROR ) {
1017 fprintf( stderr, "Subentries control encoding error!\n" );
1018 return EXIT_FAILURE;
1021 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
1022 return EXIT_FAILURE;
1025 c3.ldctl_value=(*bvalp);
1029 if ( valuesReturnFilter ) {
1033 c4.ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
1034 c4.ldctl_iscritical = valuesReturnFilter > 1;
1036 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1037 return EXIT_FAILURE;
1040 if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
1042 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
1043 return EXIT_FAILURE;
1046 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
1047 return EXIT_FAILURE;
1050 c4.ldctl_value=(*bvalp);
1053 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1058 if( err != LDAP_OPT_SUCCESS ) {
1059 fprintf( stderr, "Could not set %scontrols\n",
1060 (c1.ldctl_iscritical || c2.ldctl_iscritical)
1061 ? "critical " : "" );
1062 if( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
1063 return EXIT_FAILURE;
1069 fprintf( stderr, "filter%s: %s\nrequesting: ",
1070 infile != NULL ? " pattern" : "",
1073 if ( attrs == NULL ) {
1074 fprintf( stderr, "ALL" );
1076 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1077 fprintf( stderr, "%s ", attrs[ i ] );
1080 fprintf( stderr, "\n" );
1084 printf( "# extended LDIF\n" );
1085 } else if ( ldif < 3 ) {
1086 printf( "version: %d\n\n", 1 );
1090 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
1092 infile != NULL ? " pattern" : "",
1095 if ( attrs == NULL ) {
1098 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1099 printf( "%s ", attrs[ i ] );
1103 if ( manageDSAit ) {
1104 printf("\n# with manageDSAit %scontrol",
1105 manageDSAit > 1 ? "critical " : "" );
1108 printf("\n# with noop %scontrol",
1109 noop > 1 ? "critical " : "" );
1112 printf("\n# with subentries %scontrol: %s",
1113 subentries < 0 ? "critical " : "",
1114 abs(subentries) == 1 ? "false" : "true" );
1116 if ( valuesReturnFilter ) {
1117 printf("\n# with valuesReturnFilter %scontrol: %s",
1118 valuesReturnFilter > 1 ? "critical " : "", vrFilter );
1121 printf( "\n#\n\n" );
1124 if ( infile == NULL ) {
1125 rc = dosearch( ld, base, scope, NULL, filtpattern,
1126 attrs, attrsonly, NULL, NULL, NULL, -1 );
1131 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
1132 line[ strlen( line ) - 1 ] = '\0';
1138 rc = dosearch( ld, base, scope, filtpattern, line,
1139 attrs, attrsonly, NULL, NULL, NULL, -1 );
1141 if ( fp != stdin ) {
1151 static int dosearch(
1159 LDAPControl **sctrls,
1160 LDAPControl **cctrls,
1161 struct timeval *timeout,
1171 LDAPMessage *res, *msg;
1174 if( filtpatt != NULL ) {
1175 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1176 if( filter == NULL ) {
1178 return EXIT_FAILURE;
1181 sprintf( filter, filtpatt, value );
1184 fprintf( stderr, "filter: %s\n", filter );
1188 printf( "#\n# filter: %s\n#\n", filter );
1196 return LDAP_SUCCESS;
1199 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1200 sctrls, cctrls, timeout, sizelimit, &msgid );
1202 if ( filtpatt != NULL ) {
1206 if( rc != LDAP_SUCCESS ) {
1207 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1208 prog, ldap_err2string( rc ), rc );
1212 nresponses = nentries = nreferences = nextended = npartial = 0;
1216 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1217 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1221 (void) ldap_sort_entries( ld, &res,
1222 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1225 for ( msg = ldap_first_message( ld, res );
1227 msg = ldap_next_message( ld, msg ) )
1229 if( nresponses++ ) putchar('\n');
1231 switch( ldap_msgtype( msg ) ) {
1232 case LDAP_RES_SEARCH_ENTRY:
1234 print_entry( ld, msg, attrsonly );
1237 case LDAP_RES_SEARCH_REFERENCE:
1239 print_reference( ld, msg );
1242 case LDAP_RES_EXTENDED:
1244 print_extended( ld, msg );
1246 if( ldap_msgid( msg ) == 0 ) {
1247 /* unsolicited extended operation */
1252 case LDAP_RES_EXTENDED_PARTIAL:
1254 print_partial( ld, msg );
1257 case LDAP_RES_SEARCH_RESULT:
1258 rc = print_result( ld, msg, 1 );
1263 ldap_msgfree( res );
1267 ldap_perror( ld, "ldap_result" );
1273 printf( "\n# numResponses: %d\n", nresponses );
1274 if( nentries ) printf( "# numEntries: %d\n", nentries );
1275 if( nextended ) printf( "# numExtended: %d\n", nextended );
1276 if( npartial ) printf( "# numPartial: %d\n", npartial );
1277 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1284 /* This is the original version, the old way of doing things. */
1292 char tmpfname[ 256 ];
1295 BerElement *ber = NULL;
1296 struct berval **bvals;
1297 LDAPControl **ctrls = NULL;
1300 dn = ldap_get_dn( ld, entry );
1304 ufn = ldap_dn2ufn( dn );
1305 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1307 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1309 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1311 if( rc != LDAP_SUCCESS ) {
1312 fprintf(stderr, "print_entry: %d\n", rc );
1313 ldap_perror( ld, "ldap_get_entry_controls" );
1314 exit( EXIT_FAILURE );
1318 print_ctrls( ctrls );
1319 ldap_controls_free( ctrls );
1324 ufn = ldap_dn2ufn( dn );
1326 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1329 if( ufn != NULL ) ldap_memfree( ufn );
1332 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1333 a = ldap_next_attribute( ld, entry, ber ) )
1336 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1338 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1339 for ( i = 0; bvals[i] != NULL; i++ ) {
1340 if ( vals2tmp > 1 || ( vals2tmp
1341 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1344 /* write value to file */
1345 snprintf( tmpfname, sizeof tmpfname,
1346 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1350 tmpfd = mkstemp( tmpfname );
1357 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1362 if ( fwrite( bvals[ i ]->bv_val,
1363 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1372 snprintf( url, sizeof url, "%s%s", urlpre,
1373 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1376 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1379 write_ldif( LDIF_PUT_VALUE, a,
1380 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1383 ber_bvecfree( bvals );
1392 /* This is the proposed new way of doing things.
1393 * It is more * efficient, but the API is non-standard.
1402 char tmpfname[ 256 ];
1405 BerElement *ber = NULL;
1406 struct berval bv, *bvals, **bvp = &bvals;
1407 LDAPControl **ctrls = NULL;
1410 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1413 ufn = ldap_dn2ufn( bv.bv_val );
1414 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1416 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1418 rc = ldap_int_get_controls( ber, &ctrls );
1420 if( rc != LDAP_SUCCESS ) {
1421 fprintf(stderr, "print_entry: %d\n", rc );
1422 ldap_perror( ld, "ldap_get_entry_controls" );
1423 exit( EXIT_FAILURE );
1427 print_ctrls( ctrls );
1428 ldap_controls_free( ctrls );
1433 ufn = ldap_dn2ufn( bv.bv_val );
1435 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1438 if( ufn != NULL ) ldap_memfree( ufn );
1440 if ( attrsonly ) bvp = NULL;
1442 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ); rc == LDAP_SUCCESS;
1443 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1445 if (bv.bv_val == NULL) break;
1448 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1451 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1452 if ( vals2tmp > 1 || ( vals2tmp
1453 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1456 /* write value to file */
1457 snprintf( tmpfname, sizeof tmpfname,
1458 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1459 tmpdir, bv.bv_val );
1462 tmpfd = mkstemp( tmpfname );
1469 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1474 if ( fwrite( bvals[ i ].bv_val,
1475 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1484 snprintf( url, sizeof url, "%s%s", urlpre,
1485 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1488 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1491 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1492 bvals[ i ].bv_val, bvals[ i ].bv_len );
1495 ber_memfree( bvals );
1505 static void print_reference(
1507 LDAPMessage *reference )
1511 LDAPControl **ctrls;
1514 printf("# search reference\n");
1517 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1519 if( rc != LDAP_SUCCESS ) {
1520 ldap_perror(ld, "ldap_parse_reference");
1521 exit( EXIT_FAILURE );
1526 for( i=0; refs[i] != NULL; i++ ) {
1527 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1528 "ref", refs[i], strlen(refs[i]) );
1530 ber_memvfree( (void **) refs );
1534 print_ctrls( ctrls );
1535 ldap_controls_free( ctrls );
1539 static void print_extended(
1541 LDAPMessage *extended )
1544 char *retoid = NULL;
1545 struct berval *retdata = NULL;
1548 printf("# extended result response\n");
1551 rc = ldap_parse_extended_result( ld, extended,
1552 &retoid, &retdata, 0 );
1554 if( rc != LDAP_SUCCESS ) {
1555 ldap_perror(ld, "ldap_parse_extended_result");
1556 exit( EXIT_FAILURE );
1559 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1560 "extended", retoid, retoid ? strlen(retoid) : 0 );
1561 ber_memfree( retoid );
1564 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1565 "data", retdata->bv_val, retdata->bv_len );
1566 ber_bvfree( retdata );
1569 print_result( ld, extended, 0 );
1572 static void print_partial(
1574 LDAPMessage *partial )
1577 char *retoid = NULL;
1578 struct berval *retdata = NULL;
1579 LDAPControl **ctrls = NULL;
1582 printf("# extended partial response\n");
1585 rc = ldap_parse_extended_partial( ld, partial,
1586 &retoid, &retdata, &ctrls, 0 );
1588 if( rc != LDAP_SUCCESS ) {
1589 ldap_perror(ld, "ldap_parse_extended_partial");
1590 exit( EXIT_FAILURE );
1593 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1594 "partial", retoid, retoid ? strlen(retoid) : 0 );
1596 ber_memfree( retoid );
1599 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1601 retdata->bv_val, retdata->bv_len );
1603 ber_bvfree( retdata );
1607 print_ctrls( ctrls );
1608 ldap_controls_free( ctrls );
1612 static int print_result(
1614 LDAPMessage *result, int search )
1618 char *matcheddn = NULL;
1621 LDAPControl **ctrls = NULL;
1625 printf("# search result\n");
1628 printf("%s: %d\n", "search", ldap_msgid(result) );
1632 rc = ldap_parse_result( ld, result,
1633 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1635 if( rc != LDAP_SUCCESS ) {
1636 ldap_perror(ld, "ldap_parse_result");
1637 exit( EXIT_FAILURE );
1642 printf( "result: %d %s\n", err, ldap_err2string(err) );
1644 } else if ( err != LDAP_SUCCESS ) {
1645 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1648 if( matcheddn && *matcheddn ) {
1650 write_ldif( LDIF_PUT_VALUE,
1651 "matchedDN", matcheddn, strlen(matcheddn) );
1653 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1656 ber_memfree( matcheddn );
1659 if( text && *text ) {
1661 write_ldif( LDIF_PUT_TEXT, "text",
1662 text, strlen(text) );
1664 fprintf( stderr, "Additional information: %s\n", text );
1667 ber_memfree( text );
1672 for( i=0; refs[i] != NULL; i++ ) {
1674 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1676 fprintf( stderr, "Referral: %s\n", refs[i] );
1680 ber_memvfree( (void **) refs );
1684 print_ctrls( ctrls );
1685 ldap_controls_free( ctrls );
1691 static void print_ctrls(
1692 LDAPControl **ctrls )
1695 for(i=0; ctrls[i] != NULL; i++ ) {
1696 /* control: OID criticality base64value */
1697 struct berval *b64 = NULL;
1701 len = strlen( ctrls[i]->ldctl_oid );
1703 /* add enough for space after OID and the critical value itself */
1704 len += ctrls[i]->ldctl_iscritical
1705 ? sizeof("true") : sizeof("false");
1707 /* convert to base64 */
1708 if( ctrls[i]->ldctl_value.bv_len ) {
1709 b64 = ber_memalloc( sizeof(struct berval) );
1711 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1712 ctrls[i]->ldctl_value.bv_len ) + 1;
1713 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1715 b64->bv_len = lutil_b64_ntop(
1716 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1717 b64->bv_val, b64->bv_len );
1721 len += 1 + b64->bv_len;
1724 str = malloc( len + 1 );
1725 strcpy( str, ctrls[i]->ldctl_oid );
1726 strcat( str, ctrls[i]->ldctl_iscritical
1727 ? " true" : " false" );
1731 strcat(str, b64->bv_val );
1734 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1735 "control", str, len );
1743 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1747 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1751 fputs( ldif, stdout );
1752 ber_memfree( ldif );