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_PAGEDRESULTS
60 " [!]pr=<size> (paged results)\n"
62 #ifdef LDAP_CONTROL_SUBENTRIES
63 " [!]subentries[=true|false] (subentries)\n"
65 " -F prefix URL prefix for files (default: %s)\n"
66 " -l limit time limit (in seconds) for search\n"
67 " -L print responses in LDIFv1 format\n"
68 " -LL print responses in LDIF format without comments\n"
69 " -LLL print responses in LDIF format without comments\n"
71 " -s scope one of base, one, or sub (search scope)\n"
72 " -S attr sort the results by attribute `attr'\n"
73 " -t write binary values to files in temporary directory\n"
74 " -tt write all values to files in temporary directory\n"
75 " -T path write files to directory specified by path (default: %s)\n"
76 " -u include User Friendly entry names in the output\n"
77 " -z limit size limit (in entries) for search\n"
80 " -d level set LDAP debugging level to `level'\n"
81 " -D binddn bind DN\n"
82 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
83 " [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
84 " [!]manageDSAit (alternate form, see -M)\n"
86 " -f file read operations from `file'\n"
87 " -h host LDAP server\n"
88 " -H URI LDAP Uniform Resource Indentifier(s)\n"
89 " -I use SASL Interactive mode\n"
90 " -k use Kerberos authentication\n"
91 " -K like -k, but do only step 1 of the Kerberos bind\n"
92 " -M enable Manage DSA IT control (-MM to make critical)\n"
93 " -n show what would be done but don't actually search\n"
94 " -O props SASL security properties\n"
95 " -p port port on LDAP server\n"
96 " -P version procotol version (default: 3)\n"
97 " -Q use SASL Quiet mode\n"
98 " -R realm SASL realm\n"
99 " -U authcid SASL authentication identity\n"
100 " -v run in verbose mode (diagnostics to standard output)\n"
101 " -w passwd bind passwd (for simple authentication)\n"
102 " -W prompt for bind passwd\n"
103 " -x Simple authentication\n"
104 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
105 " -y file Read passwd from file\n"
106 " -Y mech SASL mechanism\n"
107 " -Z Start TLS request (-ZZ to require successful response)\n"
108 , s, def_urlpre, def_tmpdir );
110 exit( EXIT_FAILURE );
113 static void print_entry LDAP_P((
118 static void print_reference(
120 LDAPMessage *reference );
122 static void print_extended(
124 LDAPMessage *extended );
126 static void print_partial(
128 LDAPMessage *partial );
130 static int print_result(
135 static void print_ctrls(
136 LDAPControl **ctrls );
138 static int write_ldif LDAP_P((
144 static int dosearch LDAP_P((
152 LDAPControl **sctrls,
153 LDAPControl **cctrls,
154 struct timeval *timeout,
157 static char *tmpdir = NULL;
158 static char *urlpre = NULL;
159 static char *prog = NULL;
160 static char *binddn = NULL;
161 static struct berval passwd = { 0, NULL };
162 static char *base = NULL;
163 static char *ldaphost = NULL;
164 static char *ldapuri = NULL;
165 static int ldapport = 0;
166 #ifdef HAVE_CYRUS_SASL
167 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
168 static char *sasl_realm = NULL;
169 static char *sasl_authc_id = NULL;
170 static char *sasl_authz_id = NULL;
171 static char *sasl_mech = NULL;
172 static char *sasl_secprops = NULL;
174 static int use_tls = 0;
175 static char *sortattr = NULL;
176 static int verbose, not, includeufn, vals2tmp, ldif;
178 #ifdef LDAP_CONTROL_PAGEDRESULTS
179 static int pagedResults = 0;
180 static ber_int_t pageSize = 0;
181 static ber_int_t entriesLeft = 0;
182 static ber_int_t morePagedResults = 1;
183 static struct berval cookie = { 0, NULL };
184 static int npagedresponses;
185 static int npagedentries;
186 static int npagedreferences;
187 static int npagedextended;
188 static int npagedpartial;
190 static int parse_page_control(
193 struct berval *cookie );
194 #endif /* LDAP_CONTROL_PAGEDRESULTS */
201 if (*LDAP_DIRSEP != '/') {
202 for (p = url; *p; p++) {
203 if (*p == *LDAP_DIRSEP)
210 main( int argc, char **argv )
212 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
214 int rc, i, first, scope, deref, attrsonly, manageDSAit, noop, crit;
215 int referrals, timelimit, sizelimit, debug;
216 int authmethod, version, want_bindpw;
218 int subentries, valuesReturnFilter;
219 BerElement *ber = NULL;
220 struct berval *sebvalp = NULL, *vrbvalp = NULL;
221 char *vrFilter = NULL;
222 char *control = NULL, *cvalue;
223 char *pw_file = NULL;
224 char *authzid = NULL;
225 #ifdef LDAP_CONTROL_PAGEDRESULTS
226 struct berval *prbvalp = NULL;
228 #endif /* LDAP_CONTROL_PAGEDRESULTS */
232 debug = verbose = not = vals2tmp = referrals =
233 subentries = valuesReturnFilter =
234 attrsonly = manageDSAit = noop = ldif = want_bindpw = 0;
236 #ifdef LDAP_CONTROL_PAGEDRESULTS
237 npagedresponses = npagedentries = npagedreferences =
238 npagedextended = npagedpartial = 0;
239 #endif /* LDAP_CONTROL_PAGEDRESULTS */
241 prog = lutil_progname( "ldapsearch", argc, argv );
243 lutil_log_initialize(argc, argv);
245 deref = sizelimit = timelimit = version = -1;
247 scope = LDAP_SCOPE_SUBTREE;
250 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
251 (def_tmpdir = getenv("TMP")) == NULL &&
252 (def_tmpdir = getenv("TEMP")) == NULL )
254 def_tmpdir = LDAP_TMPDIR;
258 def_tmpdir = LDAP_TMPDIR;
260 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
262 if( def_urlpre == NULL ) {
267 sprintf( def_urlpre, "file:///%s/",
268 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
270 urlize( def_urlpre );
272 while (( i = getopt( argc, argv, "Aa:b:E:F:f:Ll:S:s:T:tuz:"
273 "Cd:e:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z")) != EOF )
277 case 'a': /* set alias deref option */
278 if ( strcasecmp( optarg, "never" ) == 0 ) {
279 deref = LDAP_DEREF_NEVER;
280 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
281 deref = LDAP_DEREF_SEARCHING;
282 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
283 deref = LDAP_DEREF_FINDING;
284 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
285 deref = LDAP_DEREF_ALWAYS;
287 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
291 case 'A': /* retrieve attribute names only -- no values */
294 case 'b': /* search base */
295 base = strdup( optarg );
297 case 'E': /* search controls */
298 if( version == LDAP_VERSION2 ) {
299 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
304 /* should be extended to support comma separated list of
305 * [!]key[=value] parameters, e.g. -E !foo,bar=567
310 if( optarg[0] == '!' ) {
315 control = strdup( optarg );
316 if ( (cvalue = strchr( control, '=' )) != NULL ) {
320 if ( strcasecmp( control, "mv" ) == 0 ) {
321 /* ValuesReturnFilter control */
322 if( valuesReturnFilter ) {
323 fprintf( stderr, "ValuesReturnFilter previously specified");
326 valuesReturnFilter= 1 + crit;
328 if ( cvalue == NULL ) {
330 "missing filter in ValuesReturnFilter control\n");
335 version = LDAP_VERSION3;
338 #ifdef LDAP_CONTROL_PAGEDRESULTS
339 } else if ( strcasecmp( control, "pr" ) == 0 ) {
341 /* PagedResults control */
342 if ( pagedResults != 0 ) {
343 fprintf( stderr, "PagedResultsControl previously specified" );
347 num = sscanf( cvalue, "%d", &tmp );
349 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", cvalue);
353 pageSize = (ber_int_t) tmp;
354 pagedResults = 1 + crit;
356 #endif /* LDAP_CONTROL_PAGEDRESULTS */
358 #ifdef LDAP_CONTROL_SUBENTRIES
359 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
361 fprintf( stderr, "subentries control previously specified");
364 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
366 } else if ( strcasecmp( cvalue, "false") == 0 ) {
370 "subentries control value \"%s\" invalid\n");
373 if( crit ) subentries *= -1;
377 fprintf( stderr, "Invalid control name: %s\n", control );
381 case 'f': /* input file */
382 if( infile != NULL ) {
383 fprintf( stderr, "%s: -f previously specified\n", prog );
386 infile = strdup( optarg );
388 case 'F': /* uri prefix */
389 if( urlpre ) free( urlpre );
390 urlpre = strdup( optarg );
392 case 'l': /* time limit */
393 timelimit = atoi( optarg );
394 if( timelimit < 0 ) {
395 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
400 case 'L': /* print entries in LDIF format */
403 case 's': /* search scope */
404 if ( strcasecmp( optarg, "base" ) == 0 ) {
405 scope = LDAP_SCOPE_BASE;
406 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
407 scope = LDAP_SCOPE_ONELEVEL;
408 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
409 scope = LDAP_SCOPE_SUBTREE;
411 fprintf( stderr, "scope should be base, one, or sub\n" );
415 case 'S': /* sort attribute */
416 sortattr = strdup( optarg );
418 case 'u': /* include UFN */
421 case 't': /* write attribute values to TMPDIR files */
424 case 'T': /* tmpdir */
425 if( tmpdir ) free( tmpdir );
426 tmpdir = strdup( optarg );
428 case 'z': /* size limit */
429 sizelimit = atoi( optarg );
437 debug |= atoi( optarg );
439 case 'D': /* bind DN */
440 if( binddn != NULL ) {
441 fprintf( stderr, "%s: -D previously specified\n", prog );
444 binddn = strdup( optarg );
446 case 'e': /* general controls */
447 if( version == LDAP_VERSION2 ) {
448 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
453 /* should be extended to support comma separated list of
454 * [!]key[=value] parameters, e.g. -e !foo,bar=567
459 if( optarg[0] == '!' ) {
464 control = strdup( optarg );
465 if ( (cvalue = strchr( control, '=' )) != NULL ) {
469 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
471 fprintf( stderr, "manageDSAit control previously specified");
474 if( cvalue != NULL ) {
475 fprintf( stderr, "manageDSAit: no control value expected" );
480 manageDSAit = 1 + crit;
483 } else if ( strcasecmp( control, "noop" ) == 0 ) {
485 fprintf( stderr, "noop control previously specified");
488 if( cvalue != NULL ) {
489 fprintf( stderr, "noop: no control value expected" );
498 fprintf( stderr, "Invalid general control name: %s\n", control );
502 case 'h': /* ldap host */
503 if( ldapuri != NULL ) {
504 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
507 if( ldaphost != NULL ) {
508 fprintf( stderr, "%s: -h previously specified\n", prog );
511 ldaphost = strdup( optarg );
513 case 'H': /* ldap URI */
514 if( ldaphost != NULL ) {
515 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
519 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
522 if( ldapuri != NULL ) {
523 fprintf( stderr, "%s: -H previously specified\n", prog );
526 ldapuri = strdup( optarg );
529 #ifdef HAVE_CYRUS_SASL
530 if( version == LDAP_VERSION2 ) {
531 fprintf( stderr, "%s: -I incompatible with version %d\n",
535 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
536 fprintf( stderr, "%s: incompatible previous "
537 "authentication choice\n",
541 authmethod = LDAP_AUTH_SASL;
542 version = LDAP_VERSION3;
543 sasl_flags = LDAP_SASL_INTERACTIVE;
546 fprintf( stderr, "%s: was not compiled with SASL support\n",
548 return( EXIT_FAILURE );
550 case 'k': /* kerberos bind */
551 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
552 if( version > LDAP_VERSION2 ) {
553 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
558 if( authmethod != -1 ) {
559 fprintf( stderr, "%s: -k incompatible with previous "
560 "authentication choice\n", prog );
564 authmethod = LDAP_AUTH_KRBV4;
566 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
570 case 'K': /* kerberos bind, part one only */
571 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
572 if( version > LDAP_VERSION2 ) {
573 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
577 if( authmethod != -1 ) {
578 fprintf( stderr, "%s: incompatible with previous "
579 "authentication choice\n", prog );
583 authmethod = LDAP_AUTH_KRBV41;
585 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
586 return( EXIT_FAILURE );
590 /* enable Manage DSA IT */
591 if( version == LDAP_VERSION2 ) {
592 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
597 version = LDAP_VERSION3;
599 case 'n': /* print deletes, don't actually do them */
603 #ifdef HAVE_CYRUS_SASL
604 if( sasl_secprops != NULL ) {
605 fprintf( stderr, "%s: -O previously specified\n", prog );
608 if( version == LDAP_VERSION2 ) {
609 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
613 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
614 fprintf( stderr, "%s: incompatible previous "
615 "authentication choice\n", prog );
618 authmethod = LDAP_AUTH_SASL;
619 version = LDAP_VERSION3;
620 sasl_secprops = strdup( optarg );
622 fprintf( stderr, "%s: not compiled with SASL support\n",
624 return( EXIT_FAILURE );
629 fprintf( stderr, "%s: -p previously specified\n", prog );
632 ldapport = atoi( optarg );
635 switch( atoi(optarg) ) {
637 if( version == LDAP_VERSION3 ) {
638 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
642 version = LDAP_VERSION2;
645 if( version == LDAP_VERSION2 ) {
646 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
650 version = LDAP_VERSION3;
653 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
656 return( EXIT_FAILURE );
659 #ifdef HAVE_CYRUS_SASL
660 if( version == LDAP_VERSION2 ) {
661 fprintf( stderr, "%s: -Q incompatible with version %d\n",
665 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
666 fprintf( stderr, "%s: incompatible previous "
667 "authentication choice\n",
671 authmethod = LDAP_AUTH_SASL;
672 version = LDAP_VERSION3;
673 sasl_flags = LDAP_SASL_QUIET;
676 fprintf( stderr, "%s: not compiled with SASL support\n",
678 return( EXIT_FAILURE );
681 #ifdef HAVE_CYRUS_SASL
682 if( sasl_realm != NULL ) {
683 fprintf( stderr, "%s: -R previously specified\n", prog );
686 if( version == LDAP_VERSION2 ) {
687 fprintf( stderr, "%s: -R incompatible with version %d\n",
691 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
692 fprintf( stderr, "%s: incompatible previous "
693 "authentication choice\n",
697 authmethod = LDAP_AUTH_SASL;
698 version = LDAP_VERSION3;
699 sasl_realm = strdup( optarg );
701 fprintf( stderr, "%s: not compiled with SASL support\n",
703 return( EXIT_FAILURE );
707 #ifdef HAVE_CYRUS_SASL
708 if( sasl_authc_id != NULL ) {
709 fprintf( stderr, "%s: -U previously specified\n", prog );
712 if( version == LDAP_VERSION2 ) {
713 fprintf( stderr, "%s: -U incompatible with version %d\n",
717 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
718 fprintf( stderr, "%s: incompatible previous "
719 "authentication choice\n",
723 authmethod = LDAP_AUTH_SASL;
724 version = LDAP_VERSION3;
725 sasl_authc_id = strdup( optarg );
727 fprintf( stderr, "%s: not compiled with SASL support\n",
729 return( EXIT_FAILURE );
732 case 'v': /* verbose mode */
735 case 'w': /* password */
736 passwd.bv_val = strdup( optarg );
740 for( p = optarg; *p != '\0'; p++ ) {
744 passwd.bv_len = strlen( passwd.bv_val );
753 #ifdef HAVE_CYRUS_SASL
754 if( sasl_mech != NULL ) {
755 fprintf( stderr, "%s: -Y previously specified\n", prog );
758 if( version == LDAP_VERSION2 ) {
759 fprintf( stderr, "%s: -Y incompatible with version %d\n",
763 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
764 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
767 authmethod = LDAP_AUTH_SASL;
768 version = LDAP_VERSION3;
769 sasl_mech = strdup( optarg );
771 fprintf( stderr, "%s: not compiled with SASL support\n",
773 return( EXIT_FAILURE );
777 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
778 fprintf( stderr, "%s: incompatible with previous "
779 "authentication choice\n", prog );
782 authmethod = LDAP_AUTH_SIMPLE;
785 #ifdef HAVE_CYRUS_SASL
786 if( sasl_authz_id != NULL ) {
787 fprintf( stderr, "%s: -X previously specified\n", prog );
790 if( version == LDAP_VERSION2 ) {
791 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
795 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
796 fprintf( stderr, "%s: -X incompatible with "
797 "authentication choice\n", prog );
800 authmethod = LDAP_AUTH_SASL;
801 version = LDAP_VERSION3;
802 sasl_authz_id = strdup( optarg );
804 fprintf( stderr, "%s: not compiled with SASL support\n",
806 return( EXIT_FAILURE );
811 if( version == LDAP_VERSION2 ) {
812 fprintf( stderr, "%s: -Z incompatible with version %d\n",
816 version = LDAP_VERSION3;
819 fprintf( stderr, "%s: not compiled with TLS support\n",
821 return( EXIT_FAILURE );
825 fprintf( stderr, "%s: unrecognized option -%c\n",
832 version = LDAP_VERSION3;
834 if (authmethod == -1 && version > LDAP_VERSION2) {
835 #ifdef HAVE_CYRUS_SASL
836 authmethod = LDAP_AUTH_SASL;
838 authmethod = LDAP_AUTH_SIMPLE;
842 if (( argc - optind < 1 ) ||
843 ( *argv[optind] != '(' /*')'*/ &&
844 ( strchr( argv[optind], '=' ) == NULL ) ) )
846 filtpattern = "(objectclass=*)";
848 filtpattern = strdup( argv[optind++] );
851 if ( argv[optind] != NULL ) {
852 attrs = &argv[optind];
855 if ( infile != NULL ) {
856 if ( infile[0] == '-' && infile[1] == '\0' ) {
858 } else if (( fp = fopen( infile, "r" )) == NULL ) {
864 if ( tmpdir == NULL ) {
867 if ( urlpre == NULL )
871 if( urlpre == NULL ) {
872 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
874 if( urlpre == NULL ) {
879 sprintf( urlpre, "file:///%s/",
880 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
886 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
887 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
889 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
890 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
896 (void) SIGNAL( SIGPIPE, SIG_IGN );
899 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
901 fprintf( stderr, "ldap_init( %s, %d )\n",
902 ldaphost != NULL ? ldaphost : "<DEFAULT>",
906 ld = ldap_init( ldaphost, ldapport );
908 perror("ldapsearch: ldap_init");
914 fprintf( stderr, "ldap_initialize( %s )\n",
915 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
918 rc = ldap_initialize( &ld, ldapuri );
919 if( rc != LDAP_SUCCESS ) {
920 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
921 rc, ldap_err2string(rc) );
927 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
929 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
932 if (timelimit != -1 &&
933 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
935 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
938 if (sizelimit != -1 &&
939 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
941 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
946 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
947 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
949 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
950 referrals ? "on" : "off" );
954 if (version == -1 ) {
955 version = LDAP_VERSION3;
958 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
959 != LDAP_OPT_SUCCESS )
961 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
966 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
967 ldap_perror( ld, "ldap_start_tls" );
973 if ( pw_file || want_bindpw ) {
975 rc = lutil_get_filed_password( pw_file, &passwd );
976 if( rc ) return EXIT_FAILURE;
978 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
979 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
983 if ( authmethod == LDAP_AUTH_SASL ) {
984 #ifdef HAVE_CYRUS_SASL
987 if( sasl_secprops != NULL ) {
988 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
989 (void *) sasl_secprops );
991 if( rc != LDAP_OPT_SUCCESS ) {
993 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
995 return( EXIT_FAILURE );
999 defaults = lutil_sasl_defaults( ld,
1006 rc = ldap_sasl_interactive_bind_s( ld, binddn,
1007 sasl_mech, NULL, NULL,
1008 sasl_flags, lutil_sasl_interact, defaults );
1010 if( rc != LDAP_SUCCESS ) {
1011 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
1012 return( EXIT_FAILURE );
1015 fprintf( stderr, "%s: not compiled with SASL support\n",
1017 return( EXIT_FAILURE );
1020 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
1022 ldap_perror( ld, "ldap_bind" );
1023 return( EXIT_FAILURE );
1027 #ifdef LDAP_CONTROL_PAGEDRESULTS
1029 if ( manageDSAit || noop || subentries || valuesReturnFilter || pageSize )
1031 if ( manageDSAit || noop || subentries || valuesReturnFilter )
1038 LDAPControl *ctrls[7];
1041 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1042 c[i].ldctl_value.bv_val = authzid;
1043 c[i].ldctl_value.bv_len = strlen( authzid );
1044 c[i].ldctl_iscritical = 1;
1046 if( c[i].ldctl_iscritical ) crit++;
1051 if ( manageDSAit ) {
1052 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1053 c[i].ldctl_value.bv_val = NULL;
1054 c[i].ldctl_value.bv_len = 0;
1055 c[i].ldctl_iscritical = manageDSAit > 1;
1057 if( c[i].ldctl_iscritical ) crit++;
1063 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1064 c[i].ldctl_value.bv_val = NULL;
1065 c[i].ldctl_value.bv_len = 0;
1066 c[i].ldctl_iscritical = noop > 1;
1068 if( c[i].ldctl_iscritical ) crit++;
1073 #ifdef LDAP_CONTROL_SUBENTRIES
1075 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1076 return EXIT_FAILURE;
1079 err = ber_printf( ber, "{b}", abs(subentries) == 1 ? 0 : 1 );
1080 if ( err == LBER_ERROR ) {
1082 fprintf( stderr, "Subentries control encoding error!\n" );
1083 return EXIT_FAILURE;
1086 if ( ber_flatten( ber, &sebvalp ) == LBER_ERROR ) {
1087 return EXIT_FAILURE;
1090 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
1091 c[i].ldctl_value=(*sebvalp);
1092 c[i].ldctl_iscritical = subentries < 1;
1094 if( c[i].ldctl_iscritical ) crit++;
1100 if ( valuesReturnFilter ) {
1101 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1102 return EXIT_FAILURE;
1105 if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
1107 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
1108 return EXIT_FAILURE;
1111 if ( ber_flatten( ber, &vrbvalp ) == LBER_ERROR ) {
1112 return EXIT_FAILURE;
1117 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
1118 c[i].ldctl_value=(*vrbvalp);
1119 c[i].ldctl_iscritical = valuesReturnFilter > 1;
1121 if( c[i].ldctl_iscritical ) crit++;
1126 #ifdef LDAP_CONTROL_PAGEDRESULTS
1127 if ( pagedResults ) {
1128 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1129 return EXIT_FAILURE;
1132 ber_printf( ber, "{iO}", pageSize, &cookie );
1133 if ( ber_flatten( ber, &prbvalp ) == LBER_ERROR ) {
1134 return EXIT_FAILURE;
1139 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1140 c[i].ldctl_value=(*prbvalp);
1141 c[i].ldctl_iscritical = pagedResults > 1;
1143 if( c[i].ldctl_iscritical ) crit++;
1147 #endif /* LDAP_CONTROL_PAGEDRESULTS */
1149 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1151 if( err != LDAP_OPT_SUCCESS ) {
1152 fprintf( stderr, "Could not set %scontrols\n",
1153 crit ? "critical " : "" );
1155 return EXIT_FAILURE;
1159 ber_bvfree( sebvalp );
1160 ber_bvfree( vrbvalp );
1161 #ifdef LDAP_CONTROL_PAGEDRESULTS
1162 ber_bvfree( prbvalp );
1163 #endif /* LDAP_CONTROL_PAGEDRESULTS */
1167 fprintf( stderr, "filter%s: %s\nrequesting: ",
1168 infile != NULL ? " pattern" : "",
1171 if ( attrs == NULL ) {
1172 fprintf( stderr, "ALL" );
1174 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1175 fprintf( stderr, "%s ", attrs[ i ] );
1178 fprintf( stderr, "\n" );
1182 printf( "# extended LDIF\n" );
1183 } else if ( ldif < 3 ) {
1184 printf( "version: %d\n\n", 1 );
1190 "# base <%s> with scope %s\n"
1194 base, (scope == LDAP_SCOPE_BASE) ? "base"
1195 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"),
1196 infile != NULL ? " pattern" : "",
1199 if ( attrs == NULL ) {
1202 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1203 printf( "%s ", attrs[ i ] );
1207 if ( manageDSAit ) {
1208 printf("\n# with manageDSAit %scontrol",
1209 manageDSAit > 1 ? "critical " : "" );
1212 printf("\n# with noop %scontrol",
1213 noop > 1 ? "critical " : "" );
1216 printf("\n# with subentries %scontrol: %s",
1217 subentries < 0 ? "critical " : "",
1218 abs(subentries) == 1 ? "false" : "true" );
1220 if ( valuesReturnFilter ) {
1221 printf("\n# with valuesReturnFilter %scontrol: %s",
1222 valuesReturnFilter > 1 ? "critical " : "", vrFilter );
1225 printf("\n# with pagedResults %scontrol: size=%d",
1226 (pagedResults > 1) ? "critical " : "",
1230 printf( "\n#\n\n" );
1233 if ( infile == NULL ) {
1234 rc = dosearch( ld, base, scope, NULL, filtpattern,
1235 attrs, attrsonly, NULL, NULL, NULL, -1 );
1240 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
1241 line[ strlen( line ) - 1 ] = '\0';
1247 rc = dosearch( ld, base, scope, filtpattern, line,
1248 attrs, attrsonly, NULL, NULL, NULL, -1 );
1250 if ( fp != stdin ) {
1255 #ifdef LDAP_CONTROL_PAGEDRESULTS
1256 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
1258 int i, moreEntries, tmpSize;
1260 /* Loop to get the next pages when
1261 * enter is pressed on the terminal.
1263 if ( entriesLeft > 0 ) {
1264 printf( "Estimate entries: %d\n", entriesLeft );
1266 printf( "Press [size] Enter for the next {%d|size} entries.\n",
1269 moreEntries = getchar();
1270 while ( moreEntries != EOF && moreEntries != '\n' ) {
1271 if ( i < sizeof(buf) - 1 ) {
1272 buf[i] = moreEntries;
1275 moreEntries = getchar();
1279 if ( i > 0 && isdigit( buf[0] ) ) {
1280 num = sscanf( buf, "%d", &tmpSize );
1282 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", buf);
1283 return EXIT_FAILURE;
1286 pageSize = (ber_int_t)tmpSize;
1291 #endif /* LDAP_CONTROL_PAGEDRESULTS */
1298 static int dosearch(
1306 LDAPControl **sctrls,
1307 LDAPControl **cctrls,
1308 struct timeval *timeout,
1318 LDAPMessage *res, *msg;
1321 if( filtpatt != NULL ) {
1322 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1323 if( filter == NULL ) {
1325 return EXIT_FAILURE;
1328 sprintf( filter, filtpatt, value );
1331 fprintf( stderr, "filter: %s\n", filter );
1335 printf( "#\n# filter: %s\n#\n", filter );
1343 return LDAP_SUCCESS;
1346 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1347 sctrls, cctrls, timeout, sizelimit, &msgid );
1349 if ( filtpatt != NULL ) {
1353 if( rc != LDAP_SUCCESS ) {
1354 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1355 prog, ldap_err2string( rc ), rc );
1359 nresponses = nentries = nreferences = nextended = npartial = 0;
1363 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1364 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1368 (void) ldap_sort_entries( ld, &res,
1369 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1372 for ( msg = ldap_first_message( ld, res );
1374 msg = ldap_next_message( ld, msg ) )
1376 if( nresponses++ ) putchar('\n');
1378 switch( ldap_msgtype( msg ) ) {
1379 case LDAP_RES_SEARCH_ENTRY:
1381 print_entry( ld, msg, attrsonly );
1384 case LDAP_RES_SEARCH_REFERENCE:
1386 print_reference( ld, msg );
1389 case LDAP_RES_EXTENDED:
1391 print_extended( ld, msg );
1393 if( ldap_msgid( msg ) == 0 ) {
1394 /* unsolicited extended operation */
1399 case LDAP_RES_EXTENDED_PARTIAL:
1401 print_partial( ld, msg );
1404 case LDAP_RES_SEARCH_RESULT:
1405 rc = print_result( ld, msg, 1 );
1406 #ifdef LDAP_CONTROL_PAGEDRESULTS
1407 if ( pageSize != 0 ) {
1408 rc = parse_page_control( ld, msg, &cookie );
1410 #endif /* LDAP_CONTROL_PAGEDRESULTS */
1415 ldap_msgfree( res );
1419 ldap_perror( ld, "ldap_result" );
1424 #ifdef LDAP_CONTROL_PAGEDRESULTS
1425 if ( pageSize != 0 ) {
1426 npagedresponses = npagedresponses + nresponses;
1427 npagedentries = npagedentries + nentries;
1428 npagedreferences = npagedreferences + nreferences;
1429 npagedextended = npagedextended + nextended;
1430 npagedpartial = npagedpartial + npartial;
1431 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
1432 printf( "\n# numResponses: %d\n", npagedresponses );
1433 if( nentries ) printf( "# numEntries: %d\n", npagedentries );
1434 if( nextended ) printf( "# numExtended: %d\n", npagedextended );
1435 if( npartial ) printf( "# numPartial: %d\n", npagedpartial );
1436 if( nreferences ) printf( "# numReferences: %d\n", npagedreferences );
1439 #endif /* LDAP_CONTROL_PAGEDRESULTS */
1441 printf( "\n# numResponses: %d\n", nresponses );
1442 if( nentries ) printf( "# numEntries: %d\n", nentries );
1443 if( nextended ) printf( "# numExtended: %d\n", nextended );
1444 if( npartial ) printf( "# numPartial: %d\n", npartial );
1445 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1452 /* This is the original version, the old way of doing things. */
1460 char tmpfname[ 256 ];
1463 BerElement *ber = NULL;
1464 struct berval **bvals;
1465 LDAPControl **ctrls = NULL;
1468 dn = ldap_get_dn( ld, entry );
1472 ufn = ldap_dn2ufn( dn );
1473 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1475 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1477 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1479 if( rc != LDAP_SUCCESS ) {
1480 fprintf(stderr, "print_entry: %d\n", rc );
1481 ldap_perror( ld, "ldap_get_entry_controls" );
1482 exit( EXIT_FAILURE );
1486 print_ctrls( ctrls );
1487 ldap_controls_free( ctrls );
1492 ufn = ldap_dn2ufn( dn );
1494 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1497 if( ufn != NULL ) ldap_memfree( ufn );
1500 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1501 a = ldap_next_attribute( ld, entry, ber ) )
1504 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1506 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1507 for ( i = 0; bvals[i] != NULL; i++ ) {
1508 if ( vals2tmp > 1 || ( vals2tmp
1509 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1512 /* write value to file */
1513 snprintf( tmpfname, sizeof tmpfname,
1514 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1518 tmpfd = mkstemp( tmpfname );
1525 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1530 if ( fwrite( bvals[ i ]->bv_val,
1531 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1540 snprintf( url, sizeof url, "%s%s", urlpre,
1541 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1544 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1547 write_ldif( LDIF_PUT_VALUE, a,
1548 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1551 ber_bvecfree( bvals );
1560 /* This is the proposed new way of doing things.
1561 * It is more efficient, but the API is non-standard.
1570 char tmpfname[ 256 ];
1573 BerElement *ber = NULL;
1574 struct berval bv, *bvals, **bvp = &bvals;
1575 LDAPControl **ctrls = NULL;
1578 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1581 ufn = ldap_dn2ufn( bv.bv_val );
1582 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1584 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1586 rc = ldap_int_get_controls( ber, &ctrls );
1588 if( rc != LDAP_SUCCESS ) {
1589 fprintf(stderr, "print_entry: %d\n", rc );
1590 ldap_perror( ld, "ldap_get_entry_controls" );
1591 exit( EXIT_FAILURE );
1595 print_ctrls( ctrls );
1596 ldap_controls_free( ctrls );
1601 ufn = ldap_dn2ufn( bv.bv_val );
1603 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1606 if( ufn != NULL ) ldap_memfree( ufn );
1608 if ( attrsonly ) bvp = NULL;
1610 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1612 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1614 if (bv.bv_val == NULL) break;
1617 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1620 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1621 if ( vals2tmp > 1 || ( vals2tmp
1622 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1625 /* write value to file */
1626 snprintf( tmpfname, sizeof tmpfname,
1627 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1628 tmpdir, bv.bv_val );
1631 tmpfd = mkstemp( tmpfname );
1638 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1643 if ( fwrite( bvals[ i ].bv_val,
1644 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1653 snprintf( url, sizeof url, "%s%s", urlpre,
1654 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1657 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1660 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1661 bvals[ i ].bv_val, bvals[ i ].bv_len );
1664 ber_memfree( bvals );
1674 static void print_reference(
1676 LDAPMessage *reference )
1680 LDAPControl **ctrls;
1683 printf("# search reference\n");
1686 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1688 if( rc != LDAP_SUCCESS ) {
1689 ldap_perror(ld, "ldap_parse_reference");
1690 exit( EXIT_FAILURE );
1695 for( i=0; refs[i] != NULL; i++ ) {
1696 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1697 "ref", refs[i], strlen(refs[i]) );
1699 ber_memvfree( (void **) refs );
1703 print_ctrls( ctrls );
1704 ldap_controls_free( ctrls );
1708 static void print_extended(
1710 LDAPMessage *extended )
1713 char *retoid = NULL;
1714 struct berval *retdata = NULL;
1717 printf("# extended result response\n");
1720 rc = ldap_parse_extended_result( ld, extended,
1721 &retoid, &retdata, 0 );
1723 if( rc != LDAP_SUCCESS ) {
1724 ldap_perror(ld, "ldap_parse_extended_result");
1725 exit( EXIT_FAILURE );
1729 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1730 "extended", retoid, retoid ? strlen(retoid) : 0 );
1732 ber_memfree( retoid );
1736 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1737 "data", retdata->bv_val, retdata->bv_len );
1739 ber_bvfree( retdata );
1742 print_result( ld, extended, 0 );
1745 static void print_partial(
1747 LDAPMessage *partial )
1750 char *retoid = NULL;
1751 struct berval *retdata = NULL;
1752 LDAPControl **ctrls = NULL;
1755 printf("# extended partial response\n");
1758 rc = ldap_parse_extended_partial( ld, partial,
1759 &retoid, &retdata, &ctrls, 0 );
1761 if( rc != LDAP_SUCCESS ) {
1762 ldap_perror(ld, "ldap_parse_extended_partial");
1763 exit( EXIT_FAILURE );
1767 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1768 "partial", retoid, retoid ? strlen(retoid) : 0 );
1771 ber_memfree( retoid );
1775 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1776 "data", retdata->bv_val, retdata->bv_len );
1779 ber_bvfree( retdata );
1783 print_ctrls( ctrls );
1784 ldap_controls_free( ctrls );
1788 static int print_result(
1790 LDAPMessage *result, int search )
1794 char *matcheddn = NULL;
1797 LDAPControl **ctrls = NULL;
1801 printf("# search result\n");
1804 printf("%s: %d\n", "search", ldap_msgid(result) );
1808 rc = ldap_parse_result( ld, result,
1809 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1811 if( rc != LDAP_SUCCESS ) {
1812 ldap_perror(ld, "ldap_parse_result");
1813 exit( EXIT_FAILURE );
1818 printf( "result: %d %s\n", err, ldap_err2string(err) );
1820 } else if ( err != LDAP_SUCCESS ) {
1821 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1824 if( matcheddn && *matcheddn ) {
1826 write_ldif( LDIF_PUT_VALUE,
1827 "matchedDN", matcheddn, strlen(matcheddn) );
1829 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1832 ber_memfree( matcheddn );
1835 if( text && *text ) {
1837 write_ldif( LDIF_PUT_TEXT, "text",
1838 text, strlen(text) );
1840 fprintf( stderr, "Additional information: %s\n", text );
1843 ber_memfree( text );
1848 for( i=0; refs[i] != NULL; i++ ) {
1850 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1852 fprintf( stderr, "Referral: %s\n", refs[i] );
1856 ber_memvfree( (void **) refs );
1860 print_ctrls( ctrls );
1861 ldap_controls_free( ctrls );
1867 static void print_ctrls(
1868 LDAPControl **ctrls )
1871 for(i=0; ctrls[i] != NULL; i++ ) {
1872 /* control: OID criticality base64value */
1873 struct berval *b64 = NULL;
1878 len += strlen( ctrls[i]->ldctl_oid );
1880 /* add enough for space after OID and the critical value itself */
1881 len += ctrls[i]->ldctl_iscritical
1882 ? sizeof("true") : sizeof("false");
1884 /* convert to base64 */
1885 if( ctrls[i]->ldctl_value.bv_len ) {
1886 b64 = ber_memalloc( sizeof(struct berval) );
1888 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1889 ctrls[i]->ldctl_value.bv_len ) + 1;
1890 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1892 b64->bv_len = lutil_b64_ntop(
1893 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1894 b64->bv_val, b64->bv_len );
1898 len += 1 + b64->bv_len;
1901 str = malloc( len + 1 );
1903 strcpy( str, ": " );
1907 strcat( str, ctrls[i]->ldctl_oid );
1908 strcat( str, ctrls[i]->ldctl_iscritical
1909 ? " true" : " false" );
1913 strcat(str, b64->bv_val );
1917 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1918 "control", str, len );
1927 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1931 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1935 fputs( ldif, stdout );
1936 ber_memfree( ldif );
1942 #ifdef LDAP_CONTROL_PAGEDRESULTS
1946 LDAPMessage *result,
1947 struct berval *cookie )
1951 LDAPControl **ctrl = NULL;
1952 LDAPControl *ctrlp = NULL;
1955 struct berval servercookie = { 0, NULL };
1958 rc = ldap_parse_result( ld, result,
1959 &err, NULL, NULL, NULL, &ctrl, 0 );
1961 if( rc != LDAP_SUCCESS ) {
1962 ldap_perror(ld, "ldap_parse_result");
1963 exit( EXIT_FAILURE );
1966 if ( err != LDAP_SUCCESS ) {
1967 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1971 /* Parse the control value
1972 * searchResult ::= SEQUENCE {
1973 * size INTEGER (0..maxInt),
1974 * -- result set size estimate from server - unused
1975 * cookie OCTET STRING
1978 ber = ber_init( &ctrlp->ldctl_value );
1979 if ( ber == NULL ) {
1980 fprintf( stderr, "Internal error.\n" );
1981 return EXIT_FAILURE;
1984 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1985 ber_dupbv( cookie, &servercookie );
1986 (void) ber_free( ber, 1 );
1988 if( tag == LBER_ERROR ) {
1989 fprintf( stderr, "Paged results response control could not be decoded.\n" );
1990 return EXIT_FAILURE;
1993 if( entriesLeft < 0 ) {
1994 fprintf( stderr, "Invalid entries estimate in paged results response.\n" );
1995 return EXIT_FAILURE;
1999 ldap_controls_free( ctrl );
2001 morePagedResults = 0;
2006 #endif /* LDAP_CONTROL_PAGEDRESULTS */