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 " [!]pr=<size> (paged results)\n"
60 #ifdef LDAP_CONTROL_SUBENTRIES
61 " [!]subentries[=true|false] (subentries)\n"
63 " -F prefix URL prefix for files (default: %s)\n"
64 " -l limit time limit (in seconds) for search\n"
65 " -L print responses in LDIFv1 format\n"
66 " -LL print responses in LDIF format without comments\n"
67 " -LLL print responses in LDIF format without comments\n"
69 " -s scope one of base, one, or sub (search scope)\n"
70 " -S attr sort the results by attribute `attr'\n"
71 " -t write binary values to files in temporary directory\n"
72 " -tt write all values to files in temporary directory\n"
73 " -T path write files to directory specified by path (default: %s)\n"
74 " -u include User Friendly entry names in the output\n"
75 " -z limit size limit (in entries) for search\n"
78 " -d level set LDAP debugging level to `level'\n"
79 " -D binddn bind DN\n"
80 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
81 " [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
82 " [!]manageDSAit (alternate form, see -M)\n"
84 " -f file read operations from `file'\n"
85 " -h host LDAP server\n"
86 " -H URI LDAP Uniform Resource Indentifier(s)\n"
87 " -I use SASL Interactive mode\n"
88 " -k use Kerberos authentication\n"
89 " -K like -k, but do only step 1 of the Kerberos bind\n"
90 " -M enable Manage DSA IT control (-MM to make critical)\n"
91 " -n show what would be done but don't actually search\n"
92 " -O props SASL security properties\n"
93 " -p port port on LDAP server\n"
94 " -P version procotol version (default: 3)\n"
95 " -Q use SASL Quiet mode\n"
96 " -R realm SASL realm\n"
97 " -U authcid SASL authentication identity\n"
98 " -v run in verbose mode (diagnostics to standard output)\n"
99 " -w passwd bind passwd (for simple authentication)\n"
100 " -W prompt for bind passwd\n"
101 " -x Simple authentication\n"
102 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
103 " -y file Read passwd from file\n"
104 " -Y mech SASL mechanism\n"
105 " -Z Start TLS request (-ZZ to require successful response)\n"
106 , s, def_urlpre, def_tmpdir );
108 exit( EXIT_FAILURE );
111 static void print_entry LDAP_P((
116 static void print_reference(
118 LDAPMessage *reference );
120 static void print_extended(
122 LDAPMessage *extended );
124 static void print_partial(
126 LDAPMessage *partial );
128 static int print_result(
133 static void print_ctrls(
134 LDAPControl **ctrls );
136 static int write_ldif LDAP_P((
142 static int dosearch LDAP_P((
150 LDAPControl **sctrls,
151 LDAPControl **cctrls,
152 struct timeval *timeout,
155 static char *tmpdir = NULL;
156 static char *urlpre = NULL;
157 static char *prog = NULL;
158 static char *binddn = NULL;
159 static struct berval passwd = { 0, NULL };
160 static char *base = NULL;
161 static char *ldaphost = NULL;
162 static char *ldapuri = NULL;
163 static int ldapport = 0;
164 #ifdef HAVE_CYRUS_SASL
165 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
166 static char *sasl_realm = NULL;
167 static char *sasl_authc_id = NULL;
168 static char *sasl_authz_id = NULL;
169 static char *sasl_mech = NULL;
170 static char *sasl_secprops = NULL;
172 static int use_tls = 0;
173 static char *sortattr = NULL;
174 static int verbose, not, includeufn, vals2tmp, ldif;
176 static int pagedResults = 0;
177 static ber_int_t pageSize = 0;
178 static ber_int_t entriesLeft = 0;
179 static ber_int_t morePagedResults = 1;
180 static struct berval cookie = { 0, NULL };
181 static int npagedresponses;
182 static int npagedentries;
183 static int npagedreferences;
184 static int npagedextended;
185 static int npagedpartial;
187 static int parse_page_control(
190 struct berval *cookie );
197 if (*LDAP_DIRSEP != '/') {
198 for (p = url; *p; p++) {
199 if (*p == *LDAP_DIRSEP)
206 main( int argc, char **argv )
208 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
210 int rc, i, first, scope, deref, attrsonly, manageDSAit, noop, crit;
211 int referrals, timelimit, sizelimit, debug;
212 int authmethod, version, want_bindpw;
214 BerElement *ber = NULL;
215 char *control = NULL, *cvalue;
216 int subentries, valuesReturnFilter;
217 struct berval *sebvalp = NULL, *vrbvalp = NULL;
218 char *vrFilter = NULL;
219 char *pw_file = NULL;
220 char *authzid = NULL;
221 struct berval *prbvalp = NULL;
224 debug = verbose = not = vals2tmp = referrals =
225 subentries = valuesReturnFilter =
226 attrsonly = manageDSAit = noop = ldif = want_bindpw = 0;
228 npagedresponses = npagedentries = npagedreferences =
229 npagedextended = npagedpartial = 0;
231 prog = lutil_progname( "ldapsearch", argc, argv );
233 lutil_log_initialize(argc, argv);
235 deref = sizelimit = timelimit = version = -1;
237 scope = LDAP_SCOPE_SUBTREE;
240 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
241 (def_tmpdir = getenv("TMP")) == NULL &&
242 (def_tmpdir = getenv("TEMP")) == NULL )
244 def_tmpdir = LDAP_TMPDIR;
248 def_tmpdir = LDAP_TMPDIR;
250 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
252 if( def_urlpre == NULL ) {
257 sprintf( def_urlpre, "file:///%s/",
258 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
260 urlize( def_urlpre );
262 while (( i = getopt( argc, argv, "Aa:b:E:F:f:Ll:S:s:T:tuz:"
263 "Cd:e:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z")) != EOF )
267 case 'a': /* set alias deref option */
268 if ( strcasecmp( optarg, "never" ) == 0 ) {
269 deref = LDAP_DEREF_NEVER;
270 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
271 deref = LDAP_DEREF_SEARCHING;
272 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
273 deref = LDAP_DEREF_FINDING;
274 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
275 deref = LDAP_DEREF_ALWAYS;
277 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
281 case 'A': /* retrieve attribute names only -- no values */
284 case 'b': /* search base */
285 base = strdup( optarg );
287 case 'E': /* search controls */
288 if( version == LDAP_VERSION2 ) {
289 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
294 /* should be extended to support comma separated list of
295 * [!]key[=value] parameters, e.g. -E !foo,bar=567
300 if( optarg[0] == '!' ) {
305 control = strdup( optarg );
306 if ( (cvalue = strchr( control, '=' )) != NULL ) {
310 if ( strcasecmp( control, "mv" ) == 0 ) {
311 /* ValuesReturnFilter control */
312 if( valuesReturnFilter ) {
313 fprintf( stderr, "ValuesReturnFilter previously specified\n");
316 valuesReturnFilter= 1 + crit;
318 if ( cvalue == NULL ) {
320 "missing filter in ValuesReturnFilter control\n");
325 version = LDAP_VERSION3;
327 } else if ( strcasecmp( control, "pr" ) == 0 ) {
329 /* PagedResults control */
330 if ( pagedResults != 0 ) {
331 fprintf( stderr, "PagedResultsControl previously specified\n" );
335 num = sscanf( cvalue, "%d", &tmp );
337 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", cvalue);
341 pageSize = (ber_int_t) tmp;
342 pagedResults = 1 + crit;
344 #ifdef LDAP_CONTROL_SUBENTRIES
345 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
347 fprintf( stderr, "subentries control previously specified\n");
350 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
352 } else if ( strcasecmp( cvalue, "false") == 0 ) {
356 "subentries control value \"%s\" invalid\n");
359 if( crit ) subentries *= -1;
363 fprintf( stderr, "Invalid control name: %s\n", control );
369 case 'f': /* input file */
370 if( infile != NULL ) {
371 fprintf( stderr, "%s: -f previously specified\n", prog );
374 infile = strdup( optarg );
376 case 'F': /* uri prefix */
377 if( urlpre ) free( urlpre );
378 urlpre = strdup( optarg );
380 case 'l': /* time limit */
381 timelimit = atoi( optarg );
382 if( timelimit < 0 ) {
383 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
388 case 'L': /* print entries in LDIF format */
391 case 's': /* search scope */
392 if ( strcasecmp( optarg, "base" ) == 0 ) {
393 scope = LDAP_SCOPE_BASE;
394 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
395 scope = LDAP_SCOPE_ONELEVEL;
396 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
397 scope = LDAP_SCOPE_SUBTREE;
399 fprintf( stderr, "scope should be base, one, or sub\n" );
403 case 'S': /* sort attribute */
404 sortattr = strdup( optarg );
406 case 'u': /* include UFN */
409 case 't': /* write attribute values to TMPDIR files */
412 case 'T': /* tmpdir */
413 if( tmpdir ) free( tmpdir );
414 tmpdir = strdup( optarg );
416 case 'z': /* size limit */
417 sizelimit = atoi( optarg );
425 debug |= atoi( optarg );
427 case 'D': /* bind DN */
428 if( binddn != NULL ) {
429 fprintf( stderr, "%s: -D previously specified\n", prog );
432 binddn = strdup( optarg );
434 case 'e': /* general controls */
435 if( version == LDAP_VERSION2 ) {
436 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
441 /* should be extended to support comma separated list of
442 * [!]key[=value] parameters, e.g. -e !foo,bar=567
447 if( optarg[0] == '!' ) {
452 control = strdup( optarg );
453 if ( (cvalue = strchr( control, '=' )) != NULL ) {
457 if ( strcasecmp( control, "authzid" ) == 0 ) {
458 if( authzid != NULL ) {
459 fprintf( stderr, "authzid control previously specified");
462 if( cvalue == NULL ) {
463 fprintf( stderr, "authzid: control value expected" );
468 fprintf( stderr, "authzid: must be marked critical" );
473 assert( authzid == NULL );
477 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
479 fprintf( stderr, "manageDSAit control previously specified");
482 if( cvalue != NULL ) {
483 fprintf( stderr, "manageDSAit: no control value expected" );
488 manageDSAit = 1 + crit;
491 } else if ( strcasecmp( control, "noop" ) == 0 ) {
493 fprintf( stderr, "noop control previously specified");
496 if( cvalue != NULL ) {
497 fprintf( stderr, "noop: no control value expected" );
506 fprintf( stderr, "Invalid general control name: %s\n", control );
510 case 'h': /* ldap host */
511 if( ldapuri != NULL ) {
512 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
515 if( ldaphost != NULL ) {
516 fprintf( stderr, "%s: -h previously specified\n", prog );
519 ldaphost = strdup( optarg );
521 case 'H': /* ldap URI */
522 if( ldaphost != NULL ) {
523 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
527 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
530 if( ldapuri != NULL ) {
531 fprintf( stderr, "%s: -H previously specified\n", prog );
534 ldapuri = strdup( optarg );
537 #ifdef HAVE_CYRUS_SASL
538 if( version == LDAP_VERSION2 ) {
539 fprintf( stderr, "%s: -I incompatible with version %d\n",
543 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
544 fprintf( stderr, "%s: incompatible previous "
545 "authentication choice\n",
549 authmethod = LDAP_AUTH_SASL;
550 version = LDAP_VERSION3;
551 sasl_flags = LDAP_SASL_INTERACTIVE;
554 fprintf( stderr, "%s: was not compiled with SASL support\n",
556 return( EXIT_FAILURE );
558 case 'k': /* kerberos bind */
559 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
560 if( version > LDAP_VERSION2 ) {
561 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
566 if( authmethod != -1 ) {
567 fprintf( stderr, "%s: -k incompatible with previous "
568 "authentication choice\n", prog );
572 authmethod = LDAP_AUTH_KRBV4;
574 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
578 case 'K': /* kerberos bind, part one only */
579 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
580 if( version > LDAP_VERSION2 ) {
581 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
585 if( authmethod != -1 ) {
586 fprintf( stderr, "%s: incompatible with previous "
587 "authentication choice\n", prog );
591 authmethod = LDAP_AUTH_KRBV41;
593 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
594 return( EXIT_FAILURE );
598 /* enable Manage DSA IT */
599 if( version == LDAP_VERSION2 ) {
600 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
605 version = LDAP_VERSION3;
607 case 'n': /* print deletes, don't actually do them */
611 #ifdef HAVE_CYRUS_SASL
612 if( sasl_secprops != NULL ) {
613 fprintf( stderr, "%s: -O previously specified\n", prog );
616 if( version == LDAP_VERSION2 ) {
617 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
621 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
622 fprintf( stderr, "%s: incompatible previous "
623 "authentication choice\n", prog );
626 authmethod = LDAP_AUTH_SASL;
627 version = LDAP_VERSION3;
628 sasl_secprops = strdup( optarg );
630 fprintf( stderr, "%s: not compiled with SASL support\n",
632 return( EXIT_FAILURE );
637 fprintf( stderr, "%s: -p previously specified\n", prog );
640 ldapport = atoi( optarg );
643 switch( atoi(optarg) ) {
645 if( version == LDAP_VERSION3 ) {
646 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
650 version = LDAP_VERSION2;
653 if( version == LDAP_VERSION2 ) {
654 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
658 version = LDAP_VERSION3;
661 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
664 return( EXIT_FAILURE );
667 #ifdef HAVE_CYRUS_SASL
668 if( version == LDAP_VERSION2 ) {
669 fprintf( stderr, "%s: -Q incompatible with version %d\n",
673 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
674 fprintf( stderr, "%s: incompatible previous "
675 "authentication choice\n",
679 authmethod = LDAP_AUTH_SASL;
680 version = LDAP_VERSION3;
681 sasl_flags = LDAP_SASL_QUIET;
684 fprintf( stderr, "%s: not compiled with SASL support\n",
686 return( EXIT_FAILURE );
689 #ifdef HAVE_CYRUS_SASL
690 if( sasl_realm != NULL ) {
691 fprintf( stderr, "%s: -R previously specified\n", prog );
694 if( version == LDAP_VERSION2 ) {
695 fprintf( stderr, "%s: -R incompatible with version %d\n",
699 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
700 fprintf( stderr, "%s: incompatible previous "
701 "authentication choice\n",
705 authmethod = LDAP_AUTH_SASL;
706 version = LDAP_VERSION3;
707 sasl_realm = strdup( optarg );
709 fprintf( stderr, "%s: not compiled with SASL support\n",
711 return( EXIT_FAILURE );
715 #ifdef HAVE_CYRUS_SASL
716 if( sasl_authc_id != NULL ) {
717 fprintf( stderr, "%s: -U previously specified\n", prog );
720 if( version == LDAP_VERSION2 ) {
721 fprintf( stderr, "%s: -U incompatible with version %d\n",
725 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
726 fprintf( stderr, "%s: incompatible previous "
727 "authentication choice\n",
731 authmethod = LDAP_AUTH_SASL;
732 version = LDAP_VERSION3;
733 sasl_authc_id = strdup( optarg );
735 fprintf( stderr, "%s: not compiled with SASL support\n",
737 return( EXIT_FAILURE );
740 case 'v': /* verbose mode */
743 case 'w': /* password */
744 passwd.bv_val = strdup( optarg );
748 for( p = optarg; *p != '\0'; p++ ) {
752 passwd.bv_len = strlen( passwd.bv_val );
761 #ifdef HAVE_CYRUS_SASL
762 if( sasl_mech != NULL ) {
763 fprintf( stderr, "%s: -Y previously specified\n", prog );
766 if( version == LDAP_VERSION2 ) {
767 fprintf( stderr, "%s: -Y incompatible with version %d\n",
771 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
772 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
775 authmethod = LDAP_AUTH_SASL;
776 version = LDAP_VERSION3;
777 sasl_mech = strdup( optarg );
779 fprintf( stderr, "%s: not compiled with SASL support\n",
781 return( EXIT_FAILURE );
785 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
786 fprintf( stderr, "%s: incompatible with previous "
787 "authentication choice\n", prog );
790 authmethod = LDAP_AUTH_SIMPLE;
793 #ifdef HAVE_CYRUS_SASL
794 if( sasl_authz_id != NULL ) {
795 fprintf( stderr, "%s: -X previously specified\n", prog );
798 if( version == LDAP_VERSION2 ) {
799 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
803 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
804 fprintf( stderr, "%s: -X incompatible with "
805 "authentication choice\n", prog );
808 authmethod = LDAP_AUTH_SASL;
809 version = LDAP_VERSION3;
810 sasl_authz_id = strdup( optarg );
812 fprintf( stderr, "%s: not compiled with SASL support\n",
814 return( EXIT_FAILURE );
819 if( version == LDAP_VERSION2 ) {
820 fprintf( stderr, "%s: -Z incompatible with version %d\n",
824 version = LDAP_VERSION3;
827 fprintf( stderr, "%s: not compiled with TLS support\n",
829 return( EXIT_FAILURE );
833 fprintf( stderr, "%s: unrecognized option -%c\n",
840 version = LDAP_VERSION3;
842 if (authmethod == -1 && version > LDAP_VERSION2) {
843 #ifdef HAVE_CYRUS_SASL
844 authmethod = LDAP_AUTH_SASL;
846 authmethod = LDAP_AUTH_SIMPLE;
850 if (( argc - optind < 1 ) ||
851 ( *argv[optind] != '(' /*')'*/ &&
852 ( strchr( argv[optind], '=' ) == NULL ) ) )
854 filtpattern = "(objectclass=*)";
856 filtpattern = strdup( argv[optind++] );
859 if ( argv[optind] != NULL ) {
860 attrs = &argv[optind];
863 if ( infile != NULL ) {
864 if ( infile[0] == '-' && infile[1] == '\0' ) {
866 } else if (( fp = fopen( infile, "r" )) == NULL ) {
872 if ( tmpdir == NULL ) {
875 if ( urlpre == NULL )
879 if( urlpre == NULL ) {
880 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
882 if( urlpre == NULL ) {
887 sprintf( urlpre, "file:///%s/",
888 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
894 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
895 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
897 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
898 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
904 (void) SIGNAL( SIGPIPE, SIG_IGN );
907 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
909 fprintf( stderr, "ldap_init( %s, %d )\n",
910 ldaphost != NULL ? ldaphost : "<DEFAULT>",
914 ld = ldap_init( ldaphost, ldapport );
916 perror("ldapsearch: ldap_init");
922 fprintf( stderr, "ldap_initialize( %s )\n",
923 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
926 rc = ldap_initialize( &ld, ldapuri );
927 if( rc != LDAP_SUCCESS ) {
928 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
929 rc, ldap_err2string(rc) );
935 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
937 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
940 if (timelimit != -1 &&
941 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
943 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
946 if (sizelimit != -1 &&
947 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
949 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
954 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
955 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
957 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
958 referrals ? "on" : "off" );
962 if (version == -1 ) {
963 version = LDAP_VERSION3;
966 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
967 != LDAP_OPT_SUCCESS )
969 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
974 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
975 ldap_perror( ld, "ldap_start_tls" );
981 if ( pw_file || want_bindpw ) {
983 rc = lutil_get_filed_password( pw_file, &passwd );
984 if( rc ) return EXIT_FAILURE;
986 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
987 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
991 if ( authmethod == LDAP_AUTH_SASL ) {
992 #ifdef HAVE_CYRUS_SASL
995 if( sasl_secprops != NULL ) {
996 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
997 (void *) sasl_secprops );
999 if( rc != LDAP_OPT_SUCCESS ) {
1001 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1003 return( EXIT_FAILURE );
1007 defaults = lutil_sasl_defaults( ld,
1014 rc = ldap_sasl_interactive_bind_s( ld, binddn,
1015 sasl_mech, NULL, NULL,
1016 sasl_flags, lutil_sasl_interact, defaults );
1018 if( rc != LDAP_SUCCESS ) {
1019 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
1020 return( EXIT_FAILURE );
1023 fprintf( stderr, "%s: not compiled with SASL support\n",
1025 return( EXIT_FAILURE );
1028 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
1030 ldap_perror( ld, "ldap_bind" );
1031 return( EXIT_FAILURE );
1036 if ( manageDSAit || noop || subentries
1037 || valuesReturnFilter || pageSize )
1043 LDAPControl *ctrls[7];
1046 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1047 c[i].ldctl_value.bv_val = authzid;
1048 c[i].ldctl_value.bv_len = strlen( authzid );
1049 c[i].ldctl_iscritical = 1;
1051 if( c[i].ldctl_iscritical ) crit++;
1056 if ( manageDSAit ) {
1057 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1058 c[i].ldctl_value.bv_val = NULL;
1059 c[i].ldctl_value.bv_len = 0;
1060 c[i].ldctl_iscritical = manageDSAit > 1;
1062 if( c[i].ldctl_iscritical ) crit++;
1068 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1069 c[i].ldctl_value.bv_val = NULL;
1070 c[i].ldctl_value.bv_len = 0;
1071 c[i].ldctl_iscritical = noop > 1;
1073 if( c[i].ldctl_iscritical ) crit++;
1078 #ifdef LDAP_CONTROL_SUBENTRIES
1080 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1081 return EXIT_FAILURE;
1084 err = ber_printf( ber, "{b}", abs(subentries) == 1 ? 0 : 1 );
1085 if ( err == LBER_ERROR ) {
1087 fprintf( stderr, "Subentries control encoding error!\n" );
1088 return EXIT_FAILURE;
1091 if ( ber_flatten( ber, &sebvalp ) == LBER_ERROR ) {
1092 return EXIT_FAILURE;
1095 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
1096 c[i].ldctl_value=(*sebvalp);
1097 c[i].ldctl_iscritical = subentries < 1;
1099 if( c[i].ldctl_iscritical ) crit++;
1105 if ( valuesReturnFilter ) {
1106 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1107 return EXIT_FAILURE;
1110 if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
1112 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
1113 return EXIT_FAILURE;
1116 if ( ber_flatten( ber, &vrbvalp ) == LBER_ERROR ) {
1117 return EXIT_FAILURE;
1122 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
1123 c[i].ldctl_value=(*vrbvalp);
1124 c[i].ldctl_iscritical = valuesReturnFilter > 1;
1126 if( c[i].ldctl_iscritical ) crit++;
1131 if ( pagedResults ) {
1132 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1133 return EXIT_FAILURE;
1136 ber_printf( ber, "{iO}", pageSize, &cookie );
1137 if ( ber_flatten( ber, &prbvalp ) == LBER_ERROR ) {
1138 return EXIT_FAILURE;
1143 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1144 c[i].ldctl_value=(*prbvalp);
1145 c[i].ldctl_iscritical = pagedResults > 1;
1147 if( c[i].ldctl_iscritical ) crit++;
1152 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1154 if( err != LDAP_OPT_SUCCESS ) {
1155 fprintf( stderr, "Could not set %scontrols\n",
1156 crit ? "critical " : "" );
1158 return EXIT_FAILURE;
1162 ber_bvfree( sebvalp );
1163 ber_bvfree( vrbvalp );
1164 ber_bvfree( prbvalp );
1168 fprintf( stderr, "filter%s: %s\nrequesting: ",
1169 infile != NULL ? " pattern" : "",
1172 if ( attrs == NULL ) {
1173 fprintf( stderr, "ALL" );
1175 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1176 fprintf( stderr, "%s ", attrs[ i ] );
1179 fprintf( stderr, "\n" );
1183 printf( "# extended LDIF\n" );
1184 } else if ( ldif < 3 ) {
1185 printf( "version: %d\n\n", 1 );
1191 "# base <%s> with scope %s\n"
1195 base, (scope == LDAP_SCOPE_BASE) ? "base"
1196 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"),
1197 infile != NULL ? " pattern" : "",
1200 if ( attrs == NULL ) {
1203 for ( i = 0; attrs[ i ] != NULL; ++i ) {
1204 printf( "%s ", attrs[ i ] );
1208 if ( manageDSAit ) {
1209 printf("\n# with manageDSAit %scontrol",
1210 manageDSAit > 1 ? "critical " : "" );
1213 printf("\n# with noop %scontrol",
1214 noop > 1 ? "critical " : "" );
1217 printf("\n# with subentries %scontrol: %s",
1218 subentries < 0 ? "critical " : "",
1219 abs(subentries) == 1 ? "false" : "true" );
1221 if ( valuesReturnFilter ) {
1222 printf("\n# with valuesReturnFilter %scontrol: %s",
1223 valuesReturnFilter > 1 ? "critical " : "", vrFilter );
1226 printf("\n# with pagedResults %scontrol: size=%d",
1227 (pagedResults > 1) ? "critical " : "",
1231 printf( "\n#\n\n" );
1234 if ( infile == NULL ) {
1235 rc = dosearch( ld, base, scope, NULL, filtpattern,
1236 attrs, attrsonly, NULL, NULL, NULL, -1 );
1241 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
1242 line[ strlen( line ) - 1 ] = '\0';
1248 rc = dosearch( ld, base, scope, filtpattern, line,
1249 attrs, attrsonly, NULL, NULL, NULL, -1 );
1251 if ( fp != stdin ) {
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 int 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;
1297 static int dosearch(
1305 LDAPControl **sctrls,
1306 LDAPControl **cctrls,
1307 struct timeval *timeout,
1317 LDAPMessage *res, *msg;
1320 if( filtpatt != NULL ) {
1321 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1322 if( filter == NULL ) {
1324 return EXIT_FAILURE;
1327 sprintf( filter, filtpatt, value );
1330 fprintf( stderr, "filter: %s\n", filter );
1334 printf( "#\n# filter: %s\n#\n", filter );
1342 return LDAP_SUCCESS;
1345 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1346 sctrls, cctrls, timeout, sizelimit, &msgid );
1348 if ( filtpatt != NULL ) {
1352 if( rc != LDAP_SUCCESS ) {
1353 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1354 prog, ldap_err2string( rc ), rc );
1358 nresponses = nentries = nreferences = nextended = npartial = 0;
1362 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1363 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1367 (void) ldap_sort_entries( ld, &res,
1368 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1371 for ( msg = ldap_first_message( ld, res );
1373 msg = ldap_next_message( ld, msg ) )
1375 if( nresponses++ ) putchar('\n');
1377 switch( ldap_msgtype( msg ) ) {
1378 case LDAP_RES_SEARCH_ENTRY:
1380 print_entry( ld, msg, attrsonly );
1383 case LDAP_RES_SEARCH_REFERENCE:
1385 print_reference( ld, msg );
1388 case LDAP_RES_EXTENDED:
1390 print_extended( ld, msg );
1392 if( ldap_msgid( msg ) == 0 ) {
1393 /* unsolicited extended operation */
1398 case LDAP_RES_EXTENDED_PARTIAL:
1400 print_partial( ld, msg );
1403 case LDAP_RES_SEARCH_RESULT:
1404 rc = print_result( ld, msg, 1 );
1405 if ( pageSize != 0 ) {
1406 rc = parse_page_control( ld, msg, &cookie );
1412 ldap_msgfree( res );
1416 ldap_perror( ld, "ldap_result" );
1421 if ( pageSize != 0 ) {
1422 npagedresponses = npagedresponses + nresponses;
1423 npagedentries = npagedentries + nentries;
1424 npagedreferences = npagedreferences + nreferences;
1425 npagedextended = npagedextended + nextended;
1426 npagedpartial = npagedpartial + npartial;
1427 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
1428 printf( "\n# numResponses: %d\n", npagedresponses );
1429 if( nentries ) printf( "# numEntries: %d\n", npagedentries );
1430 if( nextended ) printf( "# numExtended: %d\n", npagedextended );
1431 if( npartial ) printf( "# numPartial: %d\n", npagedpartial );
1432 if( nreferences ) printf( "# numReferences: %d\n", npagedreferences );
1434 } else if ( ldif < 2 ) {
1435 printf( "\n# numResponses: %d\n", nresponses );
1436 if( nentries ) printf( "# numEntries: %d\n", nentries );
1437 if( nextended ) printf( "# numExtended: %d\n", nextended );
1438 if( npartial ) printf( "# numPartial: %d\n", npartial );
1439 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1446 /* This is the original version, the old way of doing things. */
1454 char tmpfname[ 256 ];
1457 BerElement *ber = NULL;
1458 struct berval **bvals;
1459 LDAPControl **ctrls = NULL;
1462 dn = ldap_get_dn( ld, entry );
1466 ufn = ldap_dn2ufn( dn );
1467 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1469 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1471 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1473 if( rc != LDAP_SUCCESS ) {
1474 fprintf(stderr, "print_entry: %d\n", rc );
1475 ldap_perror( ld, "ldap_get_entry_controls" );
1476 exit( EXIT_FAILURE );
1480 print_ctrls( ctrls );
1481 ldap_controls_free( ctrls );
1486 ufn = ldap_dn2ufn( dn );
1488 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1491 if( ufn != NULL ) ldap_memfree( ufn );
1494 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1495 a = ldap_next_attribute( ld, entry, ber ) )
1498 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1500 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1501 for ( i = 0; bvals[i] != NULL; i++ ) {
1502 if ( vals2tmp > 1 || ( vals2tmp
1503 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1506 /* write value to file */
1507 snprintf( tmpfname, sizeof tmpfname,
1508 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1512 tmpfd = mkstemp( tmpfname );
1519 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1524 if ( fwrite( bvals[ i ]->bv_val,
1525 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1534 snprintf( url, sizeof url, "%s%s", urlpre,
1535 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1538 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1541 write_ldif( LDIF_PUT_VALUE, a,
1542 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1545 ber_bvecfree( bvals );
1554 /* This is the proposed new way of doing things.
1555 * It is more efficient, but the API is non-standard.
1564 char tmpfname[ 256 ];
1567 BerElement *ber = NULL;
1568 struct berval bv, *bvals, **bvp = &bvals;
1569 LDAPControl **ctrls = NULL;
1572 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1575 ufn = ldap_dn2ufn( bv.bv_val );
1576 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1578 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1580 rc = ldap_int_get_controls( ber, &ctrls );
1582 if( rc != LDAP_SUCCESS ) {
1583 fprintf(stderr, "print_entry: %d\n", rc );
1584 ldap_perror( ld, "ldap_get_entry_controls" );
1585 exit( EXIT_FAILURE );
1589 print_ctrls( ctrls );
1590 ldap_controls_free( ctrls );
1595 ufn = ldap_dn2ufn( bv.bv_val );
1597 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1600 if( ufn != NULL ) ldap_memfree( ufn );
1602 if ( attrsonly ) bvp = NULL;
1604 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1606 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1608 if (bv.bv_val == NULL) break;
1611 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1614 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1615 if ( vals2tmp > 1 || ( vals2tmp
1616 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1619 /* write value to file */
1620 snprintf( tmpfname, sizeof tmpfname,
1621 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1622 tmpdir, bv.bv_val );
1625 tmpfd = mkstemp( tmpfname );
1632 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1637 if ( fwrite( bvals[ i ].bv_val,
1638 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1647 snprintf( url, sizeof url, "%s%s", urlpre,
1648 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1651 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1654 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1655 bvals[ i ].bv_val, bvals[ i ].bv_len );
1658 ber_memfree( bvals );
1668 static void print_reference(
1670 LDAPMessage *reference )
1674 LDAPControl **ctrls;
1677 printf("# search reference\n");
1680 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1682 if( rc != LDAP_SUCCESS ) {
1683 ldap_perror(ld, "ldap_parse_reference");
1684 exit( EXIT_FAILURE );
1689 for( i=0; refs[i] != NULL; i++ ) {
1690 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1691 "ref", refs[i], strlen(refs[i]) );
1693 ber_memvfree( (void **) refs );
1697 print_ctrls( ctrls );
1698 ldap_controls_free( ctrls );
1702 static void print_extended(
1704 LDAPMessage *extended )
1707 char *retoid = NULL;
1708 struct berval *retdata = NULL;
1711 printf("# extended result response\n");
1714 rc = ldap_parse_extended_result( ld, extended,
1715 &retoid, &retdata, 0 );
1717 if( rc != LDAP_SUCCESS ) {
1718 ldap_perror(ld, "ldap_parse_extended_result");
1719 exit( EXIT_FAILURE );
1723 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1724 "extended", retoid, retoid ? strlen(retoid) : 0 );
1726 ber_memfree( retoid );
1730 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1731 "data", retdata->bv_val, retdata->bv_len );
1733 ber_bvfree( retdata );
1736 print_result( ld, extended, 0 );
1739 static void print_partial(
1741 LDAPMessage *partial )
1744 char *retoid = NULL;
1745 struct berval *retdata = NULL;
1746 LDAPControl **ctrls = NULL;
1749 printf("# extended partial response\n");
1752 rc = ldap_parse_extended_partial( ld, partial,
1753 &retoid, &retdata, &ctrls, 0 );
1755 if( rc != LDAP_SUCCESS ) {
1756 ldap_perror(ld, "ldap_parse_extended_partial");
1757 exit( EXIT_FAILURE );
1761 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1762 "partial", retoid, retoid ? strlen(retoid) : 0 );
1765 ber_memfree( retoid );
1769 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1770 "data", retdata->bv_val, retdata->bv_len );
1773 ber_bvfree( retdata );
1777 print_ctrls( ctrls );
1778 ldap_controls_free( ctrls );
1782 static int print_result(
1784 LDAPMessage *result, int search )
1788 char *matcheddn = NULL;
1791 LDAPControl **ctrls = NULL;
1795 printf("# search result\n");
1798 printf("%s: %d\n", "search", ldap_msgid(result) );
1802 rc = ldap_parse_result( ld, result,
1803 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1805 if( rc != LDAP_SUCCESS ) {
1806 ldap_perror(ld, "ldap_parse_result");
1807 exit( EXIT_FAILURE );
1812 printf( "result: %d %s\n", err, ldap_err2string(err) );
1814 } else if ( err != LDAP_SUCCESS ) {
1815 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1818 if( matcheddn && *matcheddn ) {
1820 write_ldif( LDIF_PUT_VALUE,
1821 "matchedDN", matcheddn, strlen(matcheddn) );
1823 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1826 ber_memfree( matcheddn );
1829 if( text && *text ) {
1831 write_ldif( LDIF_PUT_TEXT, "text",
1832 text, strlen(text) );
1834 fprintf( stderr, "Additional information: %s\n", text );
1837 ber_memfree( text );
1842 for( i=0; refs[i] != NULL; i++ ) {
1844 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1846 fprintf( stderr, "Referral: %s\n", refs[i] );
1850 ber_memvfree( (void **) refs );
1854 print_ctrls( ctrls );
1855 ldap_controls_free( ctrls );
1861 static void print_ctrls(
1862 LDAPControl **ctrls )
1865 for(i=0; ctrls[i] != NULL; i++ ) {
1866 /* control: OID criticality base64value */
1867 struct berval *b64 = NULL;
1872 len += strlen( ctrls[i]->ldctl_oid );
1874 /* add enough for space after OID and the critical value itself */
1875 len += ctrls[i]->ldctl_iscritical
1876 ? sizeof("true") : sizeof("false");
1878 /* convert to base64 */
1879 if( ctrls[i]->ldctl_value.bv_len ) {
1880 b64 = ber_memalloc( sizeof(struct berval) );
1882 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1883 ctrls[i]->ldctl_value.bv_len ) + 1;
1884 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1886 b64->bv_len = lutil_b64_ntop(
1887 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1888 b64->bv_val, b64->bv_len );
1892 len += 1 + b64->bv_len;
1895 str = malloc( len + 1 );
1897 strcpy( str, ": " );
1901 strcat( str, ctrls[i]->ldctl_oid );
1902 strcat( str, ctrls[i]->ldctl_iscritical
1903 ? " true" : " false" );
1907 strcat(str, b64->bv_val );
1911 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1912 "control", str, len );
1921 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1925 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1929 fputs( ldif, stdout );
1930 ber_memfree( ldif );
1939 LDAPMessage *result,
1940 struct berval *cookie )
1944 LDAPControl **ctrl = NULL;
1945 LDAPControl *ctrlp = NULL;
1948 struct berval servercookie = { 0, NULL };
1951 rc = ldap_parse_result( ld, result,
1952 &err, NULL, NULL, NULL, &ctrl, 0 );
1954 if( rc != LDAP_SUCCESS ) {
1955 ldap_perror(ld, "ldap_parse_result");
1956 exit( EXIT_FAILURE );
1959 if ( err != LDAP_SUCCESS ) {
1960 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1964 /* Parse the control value
1965 * searchResult ::= SEQUENCE {
1966 * size INTEGER (0..maxInt),
1967 * -- result set size estimate from server - unused
1968 * cookie OCTET STRING
1971 ber = ber_init( &ctrlp->ldctl_value );
1972 if ( ber == NULL ) {
1973 fprintf( stderr, "Internal error.\n" );
1974 return EXIT_FAILURE;
1977 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1978 ber_dupbv( cookie, &servercookie );
1979 (void) ber_free( ber, 1 );
1981 if( tag == LBER_ERROR ) {
1982 fprintf( stderr, "Paged results response control could not be decoded.\n" );
1983 return EXIT_FAILURE;
1986 if( entriesLeft < 0 ) {
1987 fprintf( stderr, "Invalid entries estimate in paged results response.\n" );
1988 return EXIT_FAILURE;
1992 ldap_controls_free( ctrl );
1994 morePagedResults = 0;