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 );
476 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
478 fprintf( stderr, "manageDSAit control previously specified");
481 if( cvalue != NULL ) {
482 fprintf( stderr, "manageDSAit: no control value expected" );
487 manageDSAit = 1 + crit;
489 } else if ( strcasecmp( control, "noop" ) == 0 ) {
491 fprintf( stderr, "noop control previously specified");
494 if( cvalue != NULL ) {
495 fprintf( stderr, "noop: no control value expected" );
503 fprintf( stderr, "Invalid general control name: %s\n", control );
508 case 'h': /* ldap host */
509 if( ldapuri != NULL ) {
510 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
513 if( ldaphost != NULL ) {
514 fprintf( stderr, "%s: -h previously specified\n", prog );
517 ldaphost = strdup( optarg );
519 case 'H': /* ldap URI */
520 if( ldaphost != NULL ) {
521 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
525 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
528 if( ldapuri != NULL ) {
529 fprintf( stderr, "%s: -H previously specified\n", prog );
532 ldapuri = strdup( optarg );
535 #ifdef HAVE_CYRUS_SASL
536 if( version == LDAP_VERSION2 ) {
537 fprintf( stderr, "%s: -I incompatible with version %d\n",
541 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
542 fprintf( stderr, "%s: incompatible previous "
543 "authentication choice\n",
547 authmethod = LDAP_AUTH_SASL;
548 version = LDAP_VERSION3;
549 sasl_flags = LDAP_SASL_INTERACTIVE;
552 fprintf( stderr, "%s: was not compiled with SASL support\n",
554 return( EXIT_FAILURE );
556 case 'k': /* kerberos bind */
557 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
558 if( version > LDAP_VERSION2 ) {
559 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
564 if( authmethod != -1 ) {
565 fprintf( stderr, "%s: -k incompatible with previous "
566 "authentication choice\n", prog );
570 authmethod = LDAP_AUTH_KRBV4;
572 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
576 case 'K': /* kerberos bind, part one only */
577 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
578 if( version > LDAP_VERSION2 ) {
579 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
583 if( authmethod != -1 ) {
584 fprintf( stderr, "%s: incompatible with previous "
585 "authentication choice\n", prog );
589 authmethod = LDAP_AUTH_KRBV41;
591 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
592 return( EXIT_FAILURE );
596 /* enable Manage DSA IT */
597 if( version == LDAP_VERSION2 ) {
598 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
603 version = LDAP_VERSION3;
605 case 'n': /* print deletes, don't actually do them */
609 #ifdef HAVE_CYRUS_SASL
610 if( sasl_secprops != NULL ) {
611 fprintf( stderr, "%s: -O previously specified\n", prog );
614 if( version == LDAP_VERSION2 ) {
615 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
619 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
620 fprintf( stderr, "%s: incompatible previous "
621 "authentication choice\n", prog );
624 authmethod = LDAP_AUTH_SASL;
625 version = LDAP_VERSION3;
626 sasl_secprops = strdup( optarg );
628 fprintf( stderr, "%s: not compiled with SASL support\n",
630 return( EXIT_FAILURE );
635 fprintf( stderr, "%s: -p previously specified\n", prog );
638 ldapport = atoi( optarg );
641 switch( atoi(optarg) ) {
643 if( version == LDAP_VERSION3 ) {
644 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
648 version = LDAP_VERSION2;
651 if( version == LDAP_VERSION2 ) {
652 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
656 version = LDAP_VERSION3;
659 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
662 return( EXIT_FAILURE );
666 #ifdef HAVE_CYRUS_SASL
667 if( version == LDAP_VERSION2 ) {
668 fprintf( stderr, "%s: -Q incompatible with version %d\n",
672 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
673 fprintf( stderr, "%s: incompatible previous "
674 "authentication choice\n",
678 authmethod = LDAP_AUTH_SASL;
679 version = LDAP_VERSION3;
680 sasl_flags = LDAP_SASL_QUIET;
683 fprintf( stderr, "%s: not compiled with SASL support\n",
685 return( EXIT_FAILURE );
688 #ifdef HAVE_CYRUS_SASL
689 if( sasl_realm != NULL ) {
690 fprintf( stderr, "%s: -R previously specified\n", prog );
693 if( version == LDAP_VERSION2 ) {
694 fprintf( stderr, "%s: -R incompatible with version %d\n",
698 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
699 fprintf( stderr, "%s: incompatible previous "
700 "authentication choice\n",
704 authmethod = LDAP_AUTH_SASL;
705 version = LDAP_VERSION3;
706 sasl_realm = strdup( optarg );
708 fprintf( stderr, "%s: not compiled with SASL support\n",
710 return( EXIT_FAILURE );
714 #ifdef HAVE_CYRUS_SASL
715 if( sasl_authc_id != NULL ) {
716 fprintf( stderr, "%s: -U previously specified\n", prog );
719 if( version == LDAP_VERSION2 ) {
720 fprintf( stderr, "%s: -U incompatible with version %d\n",
724 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
725 fprintf( stderr, "%s: incompatible previous "
726 "authentication choice\n",
730 authmethod = LDAP_AUTH_SASL;
731 version = LDAP_VERSION3;
732 sasl_authc_id = strdup( optarg );
734 fprintf( stderr, "%s: not compiled with SASL support\n",
736 return( EXIT_FAILURE );
739 case 'v': /* verbose mode */
742 case 'w': /* password */
743 passwd.bv_val = strdup( optarg );
747 for( p = optarg; *p != '\0'; p++ ) {
751 passwd.bv_len = strlen( passwd.bv_val );
760 #ifdef HAVE_CYRUS_SASL
761 if( sasl_mech != NULL ) {
762 fprintf( stderr, "%s: -Y previously specified\n", prog );
765 if( version == LDAP_VERSION2 ) {
766 fprintf( stderr, "%s: -Y incompatible with version %d\n",
770 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
771 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
774 authmethod = LDAP_AUTH_SASL;
775 version = LDAP_VERSION3;
776 sasl_mech = strdup( optarg );
778 fprintf( stderr, "%s: not compiled with SASL support\n",
780 return( EXIT_FAILURE );
784 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
785 fprintf( stderr, "%s: incompatible with previous "
786 "authentication choice\n", prog );
789 authmethod = LDAP_AUTH_SIMPLE;
792 #ifdef HAVE_CYRUS_SASL
793 if( sasl_authz_id != NULL ) {
794 fprintf( stderr, "%s: -X previously specified\n", prog );
797 if( version == LDAP_VERSION2 ) {
798 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
802 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
803 fprintf( stderr, "%s: -X incompatible with "
804 "authentication choice\n", prog );
807 authmethod = LDAP_AUTH_SASL;
808 version = LDAP_VERSION3;
809 sasl_authz_id = strdup( optarg );
811 fprintf( stderr, "%s: not compiled with SASL support\n",
813 return( EXIT_FAILURE );
818 if( version == LDAP_VERSION2 ) {
819 fprintf( stderr, "%s: -Z incompatible with version %d\n",
823 version = LDAP_VERSION3;
826 fprintf( stderr, "%s: not compiled with TLS support\n",
828 return( EXIT_FAILURE );
832 fprintf( stderr, "%s: unrecognized option -%c\n",
839 version = LDAP_VERSION3;
841 if (authmethod == -1 && version > LDAP_VERSION2) {
842 #ifdef HAVE_CYRUS_SASL
843 authmethod = LDAP_AUTH_SASL;
845 authmethod = LDAP_AUTH_SIMPLE;
849 if (( argc - optind < 1 ) ||
850 ( *argv[optind] != '(' /*')'*/ &&
851 ( strchr( argv[optind], '=' ) == NULL ) ) )
853 filtpattern = "(objectclass=*)";
855 filtpattern = strdup( argv[optind++] );
858 if ( argv[optind] != NULL ) {
859 attrs = &argv[optind];
862 if ( infile != NULL ) {
863 if ( infile[0] == '-' && infile[1] == '\0' ) {
865 } else if (( fp = fopen( infile, "r" )) == NULL ) {
871 if ( tmpdir == NULL ) {
874 if ( urlpre == NULL )
878 if( urlpre == NULL ) {
879 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
881 if( urlpre == NULL ) {
886 sprintf( urlpre, "file:///%s/",
887 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
893 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
894 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
896 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
897 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
903 (void) SIGNAL( SIGPIPE, SIG_IGN );
906 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
908 fprintf( stderr, "ldap_init( %s, %d )\n",
909 ldaphost != NULL ? ldaphost : "<DEFAULT>",
913 ld = ldap_init( ldaphost, ldapport );
915 perror("ldapsearch: ldap_init");
921 fprintf( stderr, "ldap_initialize( %s )\n",
922 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
925 rc = ldap_initialize( &ld, ldapuri );
926 if( rc != LDAP_SUCCESS ) {
927 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
928 rc, ldap_err2string(rc) );
934 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
936 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
939 if (timelimit != -1 &&
940 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
942 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
945 if (sizelimit != -1 &&
946 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
948 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
953 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
954 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
956 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
957 referrals ? "on" : "off" );
961 if (version == -1 ) {
962 version = LDAP_VERSION3;
965 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
966 != LDAP_OPT_SUCCESS )
968 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
973 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
974 ldap_perror( ld, "ldap_start_tls" );
980 if ( pw_file || want_bindpw ) {
982 rc = lutil_get_filed_password( pw_file, &passwd );
983 if( rc ) return EXIT_FAILURE;
985 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
986 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
990 if ( authmethod == LDAP_AUTH_SASL ) {
991 #ifdef HAVE_CYRUS_SASL
994 if( sasl_secprops != NULL ) {
995 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
996 (void *) sasl_secprops );
998 if( rc != LDAP_OPT_SUCCESS ) {
1000 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1002 return( EXIT_FAILURE );
1006 defaults = lutil_sasl_defaults( ld,
1013 rc = ldap_sasl_interactive_bind_s( ld, binddn,
1014 sasl_mech, NULL, NULL,
1015 sasl_flags, lutil_sasl_interact, defaults );
1017 if( rc != LDAP_SUCCESS ) {
1018 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
1019 return( EXIT_FAILURE );
1022 fprintf( stderr, "%s: not compiled with SASL support\n",
1024 return( EXIT_FAILURE );
1027 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
1029 ldap_perror( ld, "ldap_bind" );
1030 return( EXIT_FAILURE );
1035 if ( manageDSAit || noop || subentries
1036 || valuesReturnFilter || pageSize )
1042 LDAPControl *ctrls[7];
1045 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1046 c[i].ldctl_value.bv_val = authzid;
1047 c[i].ldctl_value.bv_len = strlen( authzid );
1048 c[i].ldctl_iscritical = 1;
1050 if( c[i].ldctl_iscritical ) crit++;
1055 if ( manageDSAit ) {
1056 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1057 c[i].ldctl_value.bv_val = NULL;
1058 c[i].ldctl_value.bv_len = 0;
1059 c[i].ldctl_iscritical = manageDSAit > 1;
1061 if( c[i].ldctl_iscritical ) crit++;
1067 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1068 c[i].ldctl_value.bv_val = NULL;
1069 c[i].ldctl_value.bv_len = 0;
1070 c[i].ldctl_iscritical = noop > 1;
1072 if( c[i].ldctl_iscritical ) crit++;
1077 #ifdef LDAP_CONTROL_SUBENTRIES
1079 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1080 return EXIT_FAILURE;
1083 err = ber_printf( ber, "{b}", abs(subentries) == 1 ? 0 : 1 );
1084 if ( err == LBER_ERROR ) {
1086 fprintf( stderr, "Subentries control encoding error!\n" );
1087 return EXIT_FAILURE;
1090 if ( ber_flatten( ber, &sebvalp ) == LBER_ERROR ) {
1091 return EXIT_FAILURE;
1094 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
1095 c[i].ldctl_value=(*sebvalp);
1096 c[i].ldctl_iscritical = subentries < 1;
1098 if( c[i].ldctl_iscritical ) crit++;
1104 if ( valuesReturnFilter ) {
1105 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1106 return EXIT_FAILURE;
1109 if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
1111 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
1112 return EXIT_FAILURE;
1115 if ( ber_flatten( ber, &vrbvalp ) == LBER_ERROR ) {
1116 return EXIT_FAILURE;
1121 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
1122 c[i].ldctl_value=(*vrbvalp);
1123 c[i].ldctl_iscritical = valuesReturnFilter > 1;
1125 if( c[i].ldctl_iscritical ) crit++;
1130 if ( pagedResults ) {
1131 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1132 return EXIT_FAILURE;
1135 ber_printf( ber, "{iO}", pageSize, &cookie );
1136 if ( ber_flatten( ber, &prbvalp ) == LBER_ERROR ) {
1137 return EXIT_FAILURE;
1142 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1143 c[i].ldctl_value=(*prbvalp);
1144 c[i].ldctl_iscritical = pagedResults > 1;
1146 if( c[i].ldctl_iscritical ) crit++;
1151 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1153 if( err != LDAP_OPT_SUCCESS ) {
1154 fprintf( stderr, "Could not set %scontrols\n",
1155 crit ? "critical " : "" );
1157 return EXIT_FAILURE;
1161 ber_bvfree( sebvalp );
1162 ber_bvfree( vrbvalp );
1163 ber_bvfree( prbvalp );
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 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
1257 int i, moreEntries, tmpSize;
1259 /* Loop to get the next pages when
1260 * enter is pressed on the terminal.
1262 if ( entriesLeft > 0 ) {
1263 printf( "Estimate entries: %d\n", entriesLeft );
1265 printf( "Press [size] Enter for the next {%d|size} entries.\n",
1268 moreEntries = getchar();
1269 while ( moreEntries != EOF && moreEntries != '\n' ) {
1270 if ( i < sizeof(buf) - 1 ) {
1271 buf[i] = moreEntries;
1274 moreEntries = getchar();
1278 if ( i > 0 && isdigit( buf[0] ) ) {
1279 int num = sscanf( buf, "%d", &tmpSize );
1281 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", buf);
1282 return EXIT_FAILURE;
1285 pageSize = (ber_int_t)tmpSize;
1296 static int dosearch(
1304 LDAPControl **sctrls,
1305 LDAPControl **cctrls,
1306 struct timeval *timeout,
1316 LDAPMessage *res, *msg;
1319 if( filtpatt != NULL ) {
1320 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1321 if( filter == NULL ) {
1323 return EXIT_FAILURE;
1326 sprintf( filter, filtpatt, value );
1329 fprintf( stderr, "filter: %s\n", filter );
1333 printf( "#\n# filter: %s\n#\n", filter );
1341 return LDAP_SUCCESS;
1344 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1345 sctrls, cctrls, timeout, sizelimit, &msgid );
1347 if ( filtpatt != NULL ) {
1351 if( rc != LDAP_SUCCESS ) {
1352 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1353 prog, ldap_err2string( rc ), rc );
1357 nresponses = nentries = nreferences = nextended = npartial = 0;
1361 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1362 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1366 (void) ldap_sort_entries( ld, &res,
1367 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1370 for ( msg = ldap_first_message( ld, res );
1372 msg = ldap_next_message( ld, msg ) )
1374 if( nresponses++ ) putchar('\n');
1376 switch( ldap_msgtype( msg ) ) {
1377 case LDAP_RES_SEARCH_ENTRY:
1379 print_entry( ld, msg, attrsonly );
1382 case LDAP_RES_SEARCH_REFERENCE:
1384 print_reference( ld, msg );
1387 case LDAP_RES_EXTENDED:
1389 print_extended( ld, msg );
1391 if( ldap_msgid( msg ) == 0 ) {
1392 /* unsolicited extended operation */
1397 case LDAP_RES_EXTENDED_PARTIAL:
1399 print_partial( ld, msg );
1402 case LDAP_RES_SEARCH_RESULT:
1403 rc = print_result( ld, msg, 1 );
1404 if ( pageSize != 0 ) {
1405 rc = parse_page_control( ld, msg, &cookie );
1411 ldap_msgfree( res );
1415 ldap_perror( ld, "ldap_result" );
1420 if ( pageSize != 0 ) {
1421 npagedresponses = npagedresponses + nresponses;
1422 npagedentries = npagedentries + nentries;
1423 npagedreferences = npagedreferences + nreferences;
1424 npagedextended = npagedextended + nextended;
1425 npagedpartial = npagedpartial + npartial;
1426 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
1427 printf( "\n# numResponses: %d\n", npagedresponses );
1428 if( nentries ) printf( "# numEntries: %d\n", npagedentries );
1429 if( nextended ) printf( "# numExtended: %d\n", npagedextended );
1430 if( npartial ) printf( "# numPartial: %d\n", npagedpartial );
1431 if( nreferences ) printf( "# numReferences: %d\n", npagedreferences );
1433 } else if ( ldif < 2 ) {
1434 printf( "\n# numResponses: %d\n", nresponses );
1435 if( nentries ) printf( "# numEntries: %d\n", nentries );
1436 if( nextended ) printf( "# numExtended: %d\n", nextended );
1437 if( npartial ) printf( "# numPartial: %d\n", npartial );
1438 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1445 /* This is the original version, the old way of doing things. */
1453 char tmpfname[ 256 ];
1456 BerElement *ber = NULL;
1457 struct berval **bvals;
1458 LDAPControl **ctrls = NULL;
1461 dn = ldap_get_dn( ld, entry );
1465 ufn = ldap_dn2ufn( dn );
1466 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1468 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1470 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1472 if( rc != LDAP_SUCCESS ) {
1473 fprintf(stderr, "print_entry: %d\n", rc );
1474 ldap_perror( ld, "ldap_get_entry_controls" );
1475 exit( EXIT_FAILURE );
1479 print_ctrls( ctrls );
1480 ldap_controls_free( ctrls );
1485 ufn = ldap_dn2ufn( dn );
1487 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1490 if( ufn != NULL ) ldap_memfree( ufn );
1493 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1494 a = ldap_next_attribute( ld, entry, ber ) )
1497 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1499 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1500 for ( i = 0; bvals[i] != NULL; i++ ) {
1501 if ( vals2tmp > 1 || ( vals2tmp
1502 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1505 /* write value to file */
1506 snprintf( tmpfname, sizeof tmpfname,
1507 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1511 tmpfd = mkstemp( tmpfname );
1518 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1523 if ( fwrite( bvals[ i ]->bv_val,
1524 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1533 snprintf( url, sizeof url, "%s%s", urlpre,
1534 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1537 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1540 write_ldif( LDIF_PUT_VALUE, a,
1541 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1544 ber_bvecfree( bvals );
1553 /* This is the proposed new way of doing things.
1554 * It is more efficient, but the API is non-standard.
1563 char tmpfname[ 256 ];
1566 BerElement *ber = NULL;
1567 struct berval bv, *bvals, **bvp = &bvals;
1568 LDAPControl **ctrls = NULL;
1571 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1574 ufn = ldap_dn2ufn( bv.bv_val );
1575 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1577 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1579 rc = ldap_int_get_controls( ber, &ctrls );
1581 if( rc != LDAP_SUCCESS ) {
1582 fprintf(stderr, "print_entry: %d\n", rc );
1583 ldap_perror( ld, "ldap_get_entry_controls" );
1584 exit( EXIT_FAILURE );
1588 print_ctrls( ctrls );
1589 ldap_controls_free( ctrls );
1594 ufn = ldap_dn2ufn( bv.bv_val );
1596 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1599 if( ufn != NULL ) ldap_memfree( ufn );
1601 if ( attrsonly ) bvp = NULL;
1603 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1605 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1607 if (bv.bv_val == NULL) break;
1610 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1613 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1614 if ( vals2tmp > 1 || ( vals2tmp
1615 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1618 /* write value to file */
1619 snprintf( tmpfname, sizeof tmpfname,
1620 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1621 tmpdir, bv.bv_val );
1624 tmpfd = mkstemp( tmpfname );
1631 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1636 if ( fwrite( bvals[ i ].bv_val,
1637 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1646 snprintf( url, sizeof url, "%s%s", urlpre,
1647 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1650 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1653 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1654 bvals[ i ].bv_val, bvals[ i ].bv_len );
1657 ber_memfree( bvals );
1667 static void print_reference(
1669 LDAPMessage *reference )
1673 LDAPControl **ctrls;
1676 printf("# search reference\n");
1679 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1681 if( rc != LDAP_SUCCESS ) {
1682 ldap_perror(ld, "ldap_parse_reference");
1683 exit( EXIT_FAILURE );
1688 for( i=0; refs[i] != NULL; i++ ) {
1689 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1690 "ref", refs[i], strlen(refs[i]) );
1692 ber_memvfree( (void **) refs );
1696 print_ctrls( ctrls );
1697 ldap_controls_free( ctrls );
1701 static void print_extended(
1703 LDAPMessage *extended )
1706 char *retoid = NULL;
1707 struct berval *retdata = NULL;
1710 printf("# extended result response\n");
1713 rc = ldap_parse_extended_result( ld, extended,
1714 &retoid, &retdata, 0 );
1716 if( rc != LDAP_SUCCESS ) {
1717 ldap_perror(ld, "ldap_parse_extended_result");
1718 exit( EXIT_FAILURE );
1722 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1723 "extended", retoid, retoid ? strlen(retoid) : 0 );
1725 ber_memfree( retoid );
1729 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1730 "data", retdata->bv_val, retdata->bv_len );
1732 ber_bvfree( retdata );
1735 print_result( ld, extended, 0 );
1738 static void print_partial(
1740 LDAPMessage *partial )
1743 char *retoid = NULL;
1744 struct berval *retdata = NULL;
1745 LDAPControl **ctrls = NULL;
1748 printf("# extended partial response\n");
1751 rc = ldap_parse_extended_partial( ld, partial,
1752 &retoid, &retdata, &ctrls, 0 );
1754 if( rc != LDAP_SUCCESS ) {
1755 ldap_perror(ld, "ldap_parse_extended_partial");
1756 exit( EXIT_FAILURE );
1760 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1761 "partial", retoid, retoid ? strlen(retoid) : 0 );
1764 ber_memfree( retoid );
1768 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1769 "data", retdata->bv_val, retdata->bv_len );
1772 ber_bvfree( retdata );
1776 print_ctrls( ctrls );
1777 ldap_controls_free( ctrls );
1781 static int print_result(
1783 LDAPMessage *result, int search )
1787 char *matcheddn = NULL;
1790 LDAPControl **ctrls = NULL;
1794 printf("# search result\n");
1797 printf("%s: %d\n", "search", ldap_msgid(result) );
1801 rc = ldap_parse_result( ld, result,
1802 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1804 if( rc != LDAP_SUCCESS ) {
1805 ldap_perror(ld, "ldap_parse_result");
1806 exit( EXIT_FAILURE );
1811 printf( "result: %d %s\n", err, ldap_err2string(err) );
1813 } else if ( err != LDAP_SUCCESS ) {
1814 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1817 if( matcheddn && *matcheddn ) {
1819 write_ldif( LDIF_PUT_VALUE,
1820 "matchedDN", matcheddn, strlen(matcheddn) );
1822 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1825 ber_memfree( matcheddn );
1828 if( text && *text ) {
1830 write_ldif( LDIF_PUT_TEXT, "text",
1831 text, strlen(text) );
1833 fprintf( stderr, "Additional information: %s\n", text );
1836 ber_memfree( text );
1841 for( i=0; refs[i] != NULL; i++ ) {
1843 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1845 fprintf( stderr, "Referral: %s\n", refs[i] );
1849 ber_memvfree( (void **) refs );
1853 print_ctrls( ctrls );
1854 ldap_controls_free( ctrls );
1860 static void print_ctrls(
1861 LDAPControl **ctrls )
1864 for(i=0; ctrls[i] != NULL; i++ ) {
1865 /* control: OID criticality base64value */
1866 struct berval *b64 = NULL;
1871 len += strlen( ctrls[i]->ldctl_oid );
1873 /* add enough for space after OID and the critical value itself */
1874 len += ctrls[i]->ldctl_iscritical
1875 ? sizeof("true") : sizeof("false");
1877 /* convert to base64 */
1878 if( ctrls[i]->ldctl_value.bv_len ) {
1879 b64 = ber_memalloc( sizeof(struct berval) );
1881 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1882 ctrls[i]->ldctl_value.bv_len ) + 1;
1883 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1885 b64->bv_len = lutil_b64_ntop(
1886 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1887 b64->bv_val, b64->bv_len );
1891 len += 1 + b64->bv_len;
1894 str = malloc( len + 1 );
1896 strcpy( str, ": " );
1900 strcat( str, ctrls[i]->ldctl_oid );
1901 strcat( str, ctrls[i]->ldctl_iscritical
1902 ? " true" : " false" );
1906 strcat(str, b64->bv_val );
1910 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1911 "control", str, len );
1920 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1924 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1928 fputs( ldif, stdout );
1929 ber_memfree( ldif );
1938 LDAPMessage *result,
1939 struct berval *cookie )
1943 LDAPControl **ctrl = NULL;
1944 LDAPControl *ctrlp = NULL;
1947 struct berval servercookie = { 0, NULL };
1950 rc = ldap_parse_result( ld, result,
1951 &err, NULL, NULL, NULL, &ctrl, 0 );
1953 if( rc != LDAP_SUCCESS ) {
1954 ldap_perror(ld, "ldap_parse_result");
1955 exit( EXIT_FAILURE );
1958 if ( err != LDAP_SUCCESS ) {
1959 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1963 /* Parse the control value
1964 * searchResult ::= SEQUENCE {
1965 * size INTEGER (0..maxInt),
1966 * -- result set size estimate from server - unused
1967 * cookie OCTET STRING
1970 ber = ber_init( &ctrlp->ldctl_value );
1971 if ( ber == NULL ) {
1972 fprintf( stderr, "Internal error.\n" );
1973 return EXIT_FAILURE;
1976 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1977 ber_dupbv( cookie, &servercookie );
1978 (void) ber_free( ber, 1 );
1980 if( tag == LBER_ERROR ) {
1981 fprintf( stderr, "Paged results response control could not be decoded.\n" );
1982 return EXIT_FAILURE;
1985 if( entriesLeft < 0 ) {
1986 fprintf( stderr, "Invalid entries estimate in paged results response.\n" );
1987 return EXIT_FAILURE;
1991 ldap_controls_free( ctrl );
1993 morePagedResults = 0;