3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
33 #include "lutil_ldap.h"
34 #include "ldap_defaults.h"
40 static int scope = LDAP_SCOPE_SUBTREE;
41 static int deref = -1;
43 static int timelimit = -1;
44 static int sizelimit = -1;
46 static char *def_tmpdir;
47 static char *def_urlpre;
53 fprintf( stderr, _("usage: %s [options] [filter [attributes...]]\nwhere:\n"), prog);
54 fprintf( stderr, _(" filter\tRFC-2254 compliant LDAP search filter\n"));
55 fprintf( stderr, _(" attributes\twhitespace-separated list of attribute descriptions\n"));
56 fprintf( stderr, _(" which may include:\n"));
57 fprintf( stderr, _(" 1.1 no attributes\n"));
58 fprintf( stderr, _(" * all user attributes\n"));
59 fprintf( stderr, _(" + all operational attributes\n"));
62 fprintf( stderr, _("Search options:\n"));
63 fprintf( stderr, _(" -a deref one of never (default), always, search, or find\n"));
64 fprintf( stderr, _(" -A retrieve attribute names only (no values)\n"));
65 fprintf( stderr, _(" -b basedn base dn for search\n"));
66 fprintf( stderr, _(" -E [!]<ctrl>[=<ctrlparam>] search controls (! indicates criticality)\n"));
67 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
68 fprintf( stderr, _(" [!]domainScope (domain scope)\n"));
70 fprintf( stderr, _(" [!]mv=<filter> (matched values filter)\n"));
71 #ifdef LDAP_CONTROL_PAGEDRESULTS
72 fprintf( stderr, _(" [!]pr=<size> (paged results)\n"));
74 #ifdef LDAP_CONTROL_SUBENTRIES
75 fprintf( stderr, _(" [!]subentries[=true|false] (subentries)\n"));
77 #ifdef LDAP_CLIENT_UPDATE
78 fprintf( stderr, _(" [!]lcup=p/<cint>/<cookie>/<slimit> (LDAP client update)\n"));
80 * " s/<cint>/<cookie> (LDAP client update)\n"
81 * " sp/<cint>/<cookie>/<slimit>\n"
85 fprintf( stderr, _(" [!]sync=ro[/<cookie>] (LDAP Sync refreshOnly)\n"));
86 fprintf( stderr, _(" rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
88 fprintf( stderr, _(" -F prefix URL prefix for files (default: %s)\n"), def_urlpre);
89 fprintf( stderr, _(" -l limit time limit (in seconds) for search\n"));
90 fprintf( stderr, _(" -L print responses in LDIFv1 format\n"));
91 fprintf( stderr, _(" -LL print responses in LDIF format without comments\n"));
92 fprintf( stderr, _(" -LLL print responses in LDIF format without comments\n"));
93 fprintf( stderr, _(" and version\n"));
94 fprintf( stderr, _(" -s scope one of base, one, or sub (search scope)\n"));
95 fprintf( stderr, _(" -S attr sort the results by attribute `attr'\n"));
96 fprintf( stderr, _(" -t write binary values to files in temporary directory\n"));
97 fprintf( stderr, _(" -tt write all values to files in temporary directory\n"));
98 fprintf( stderr, _(" -T path write files to directory specified by path (default: %s)\n"), def_tmpdir);
99 fprintf( stderr, _(" -u include User Friendly entry names in the output\n"));
100 fprintf( stderr, _(" -z limit size limit (in entries) for search\n"));
102 exit( EXIT_FAILURE );
105 static void print_entry LDAP_P((
110 static void print_reference(
112 LDAPMessage *reference );
114 static void print_extended(
116 LDAPMessage *extended );
118 static void print_partial(
120 LDAPMessage *partial );
122 static int print_result(
127 static void print_ctrls(
128 LDAPControl **ctrls );
130 static int write_ldif LDAP_P((
136 static int dosearch LDAP_P((
144 LDAPControl **sctrls,
145 LDAPControl **cctrls,
146 struct timeval *timeout,
149 static char *tmpdir = NULL;
150 static char *urlpre = NULL;
151 static char *base = NULL;
152 static char *sortattr = NULL;
153 static int includeufn, vals2tmp = 0, ldif = 0;
155 static int subentries = 0, valuesReturnFilter = 0;
156 static char *vrFilter = NULL;
158 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
159 static int domainScope = 0;
162 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
164 static int ldapsync = 0;
167 #ifdef LDAP_CLIENT_UPDATE
168 static int lcup_cint = 0;
169 static struct berval lcup_cookie = { 0, NULL };
170 static int lcup_slimit = -1;
174 static struct berval sync_cookie = { 0, NULL };
175 static int sync_slimit = -1;
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 );
201 if (*LDAP_DIRSEP != '/') {
202 for (p = url; *p; p++) {
203 if (*p == *LDAP_DIRSEP)
210 const char options[] = "a:Ab:E:F:l:Ls:S:tT:uz:"
211 "Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
214 handle_private_option( int i )
217 char *control, *cvalue;
219 case 'a': /* set alias deref option */
220 if ( strcasecmp( optarg, "never" ) == 0 ) {
221 deref = LDAP_DEREF_NEVER;
222 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
223 deref = LDAP_DEREF_SEARCHING;
224 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
225 deref = LDAP_DEREF_FINDING;
226 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
227 deref = LDAP_DEREF_ALWAYS;
229 fprintf( stderr, _("alias deref should be never, search, find, or always\n") );
233 case 'A': /* retrieve attribute names only -- no values */
236 case 'b': /* search base */
237 base = strdup( optarg );
239 case 'E': /* search controls */
240 if( protocol == LDAP_VERSION2 ) {
241 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
243 exit( EXIT_FAILURE );
246 /* should be extended to support comma separated list of
247 * [!]key[=value] parameters, e.g. -E !foo,bar=567
252 if( optarg[0] == '!' ) {
257 control = strdup( optarg );
258 if ( (cvalue = strchr( control, '=' )) != NULL ) {
262 if ( strcasecmp( control, "mv" ) == 0 ) {
263 /* ValuesReturnFilter control */
264 if( valuesReturnFilter ) {
265 fprintf( stderr, _("ValuesReturnFilter previously specified\n"));
266 exit( EXIT_FAILURE );
268 valuesReturnFilter= 1 + crit;
270 if ( cvalue == NULL ) {
272 _("missing filter in ValuesReturnFilter control\n"));
273 exit( EXIT_FAILURE );
277 protocol = LDAP_VERSION3;
279 #ifdef LDAP_CONTROL_PAGEDRESULTS
280 } else if ( strcasecmp( control, "pr" ) == 0 ) {
282 /* PagedResults control */
283 if ( pagedResults != 0 ) {
284 fprintf( stderr, _("PagedResultsControl previously specified\n") );
285 exit( EXIT_FAILURE );
288 num = sscanf( cvalue, "%d", &tmp );
290 fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), cvalue);
291 exit( EXIT_FAILURE );
294 pageSize = (ber_int_t) tmp;
295 pagedResults = 1 + crit;
298 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
299 } else if ( strcasecmp( control, "domainScope" ) == 0 ) {
302 _("domainScope control previously specified\n"));
303 exit( EXIT_FAILURE );
305 if( cvalue != NULL ) {
307 _("domainScope: no control value expected\n") );
311 domainScope = 1 + crit;
313 #ifdef LDAP_CONTROL_SUBENTRIES
314 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
316 fprintf( stderr, _("subentries control previously specified\n"));
317 exit( EXIT_FAILURE );
319 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
321 } else if ( strcasecmp( cvalue, "false") == 0 ) {
325 _("subentries control value \"%s\" invalid\n"),
327 exit( EXIT_FAILURE );
329 if( crit ) subentries *= -1;
332 #ifdef LDAP_CLIENT_UPDATE
333 } else if ( strcasecmp( control, "lcup" ) == 0 ) {
337 fprintf( stderr, _("client update control previously specified\n"));
338 exit( EXIT_FAILURE );
340 if ( ldapsync != -1 ) {
341 fprintf( stderr, _("ldap sync control previously specified\n"));
342 exit( EXIT_FAILURE );
344 if ( cvalue == NULL ) {
346 _("missing specification of client update control\n"));
347 exit( EXIT_FAILURE );
349 if ( strncasecmp( cvalue, "p", 1 ) == 0 ) {
350 lcup = LDAP_CUP_PERSIST_ONLY;
351 cvalue = strchr( cvalue, '/' );
353 cookiep = strchr( cvalue, '/' );
355 lcup_cint = atoi( cvalue );
357 slimitp = strchr( cvalue, '/' );
359 while ( isspace( *cookiep ) ) cookiep++;
360 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
361 lcup_slimit = atoi( slimitp );
363 } else if ( strncasecmp( cvalue, "s", 1 ) == 0 ) {
364 lcup = LDAP_CUP_SYNC_ONLY;
366 cookiep = strchr( cvalue, '/' );
368 lcup_cint = atoi( cvalue );
369 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
370 } else if ( strncasecmp( cvalue, "sp", 2 ) == 0 ) {
371 lcup = LDAP_CUP_SYNC_AND_PERSIST;
373 cookiep = strchr( cvalue, '/' );
375 lcup_cint = atoi( cvalue );
377 slimitp = strchr( cvalue, '/' );
379 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
380 lcup_slimit = atoi( slimitp );
384 _("client update control value \"%s\" invalid\n"),
386 exit( EXIT_FAILURE );
388 if ( crit ) lcup *= -1;
392 } else if ( strcasecmp( control, "sync" ) == 0 ) {
396 fprintf( stderr, _("ldap sync control previously specified\n") );
397 exit( EXIT_FAILURE );
400 fprintf( stderr, _("client update control previously specified\n") );
401 exit( EXIT_FAILURE );
403 if ( cvalue == NULL ) {
405 _("missing specification of ldap sync control\n"));
406 exit( EXIT_FAILURE );
408 if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
409 ldapsync = LDAP_SYNC_REFRESH_ONLY;
410 cookiep = strchr( cvalue, '/' );
411 if ( cookiep != NULL ) {
413 if ( *cookiep != '\0' ) {
414 ber_str2bv( cookiep, 0, 0, &sync_cookie );
417 } else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
418 ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
419 cookiep = strchr( cvalue, '/' );
420 if ( cookiep != NULL ) {
424 slimitp = strchr( cvalue, '/' );
425 if ( slimitp != NULL ) {
428 if ( cookiep != NULL && *cookiep != '\0' )
429 ber_str2bv( cookiep, 0, 0, &sync_cookie );
430 if ( slimitp != NULL && *slimitp != '\0' )
431 sync_slimit = atoi( slimitp );
434 _("ldap sync control value \"%s\" invalid\n"),
436 exit( EXIT_FAILURE );
438 if ( crit ) ldapsync *= -1;
442 fprintf( stderr, _("Invalid control name: %s\n"), control );
446 case 'F': /* uri prefix */
447 if( urlpre ) free( urlpre );
448 urlpre = strdup( optarg );
450 case 'l': /* time limit */
451 timelimit = atoi( optarg );
452 if( timelimit < 0 ) {
453 fprintf( stderr, _("%s: invalid timelimit (%d) specified\n"),
455 exit( EXIT_FAILURE );
458 case 'L': /* print entries in LDIF format */
461 case 's': /* search scope */
462 if ( strcasecmp( optarg, "base" ) == 0 ) {
463 scope = LDAP_SCOPE_BASE;
464 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
465 scope = LDAP_SCOPE_ONELEVEL;
466 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
467 scope = LDAP_SCOPE_SUBTREE;
469 fprintf( stderr, _("scope should be base, one, or sub\n") );
473 case 'S': /* sort attribute */
474 sortattr = strdup( optarg );
476 case 't': /* write attribute values to TMPDIR files */
479 case 'T': /* tmpdir */
480 if( tmpdir ) free( tmpdir );
481 tmpdir = strdup( optarg );
483 case 'u': /* include UFN */
486 case 'z': /* size limit */
487 sizelimit = atoi( optarg );
497 private_conn_setup( LDAP *ld )
500 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
502 fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
503 exit( EXIT_FAILURE );
505 if (timelimit != -1 &&
506 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
508 fprintf( stderr, _("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
509 exit( EXIT_FAILURE );
511 if (sizelimit != -1 &&
512 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
514 fprintf( stderr, _("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
515 exit( EXIT_FAILURE );
521 main( int argc, char **argv )
523 char *filtpattern, **attrs = NULL, line[BUFSIZ];
527 BerElement *seber = NULL, *vrber = NULL, *prber = NULL;
528 #ifdef LDAP_CLIENT_UPDATE
529 BerElement *cuber = NULL;
530 struct berval *cubvalp = NULL;
534 BerElement *syncber = NULL;
535 struct berval *syncbvalp = NULL;
540 #ifdef LDAP_CONTROL_PAGEDRESULTS
541 npagedresponses = npagedentries = npagedreferences =
542 npagedextended = npagedpartial = 0;
545 prog = lutil_progname( "ldapsearch", argc, argv );
547 lutil_log_initialize(argc, argv);
549 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
550 (def_tmpdir = getenv("TMP")) == NULL &&
551 (def_tmpdir = getenv("TEMP")) == NULL )
553 def_tmpdir = LDAP_TMPDIR;
557 def_tmpdir = LDAP_TMPDIR;
559 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
561 if( def_urlpre == NULL ) {
566 sprintf( def_urlpre, "file:///%s/",
567 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
569 urlize( def_urlpre );
571 tool_args( argc, argv );
573 if (( argc - optind < 1 ) ||
574 ( *argv[optind] != '(' /*')'*/ &&
575 ( strchr( argv[optind], '=' ) == NULL ) ) )
577 filtpattern = "(objectclass=*)";
579 filtpattern = strdup( argv[optind++] );
582 if ( argv[optind] != NULL ) {
583 attrs = &argv[optind];
586 if ( infile != NULL ) {
587 if ( infile[0] == '-' && infile[1] == '\0' ) {
589 } else if (( fp = fopen( infile, "r" )) == NULL ) {
595 if ( tmpdir == NULL ) {
598 if ( urlpre == NULL )
602 if( urlpre == NULL ) {
603 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
605 if( urlpre == NULL ) {
610 sprintf( urlpre, "file:///%s/",
611 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
619 ld = tool_conn_setup( 0, &private_conn_setup );
621 if ( pw_file || want_bindpw ) {
623 rc = lutil_get_filed_password( pw_file, &passwd );
624 if( rc ) return EXIT_FAILURE;
626 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
627 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
634 if ( manageDSAit || noop || subentries || valuesReturnFilter
635 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
638 #ifdef LDAP_CONTROL_PAGEDRESULTS
641 #ifdef LDAP_CLIENT_UPDATE
652 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
654 c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
655 c[i].ldctl_value.bv_val = NULL;
656 c[i].ldctl_value.bv_len = 0;
657 c[i].ldctl_iscritical = domainScope > 1;
662 #ifdef LDAP_CONTROL_SUBENTRIES
664 if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
668 err = ber_printf( seber, "{b}", abs(subentries) == 1 ? 0 : 1 );
670 ber_free( seber, 1 );
671 fprintf( stderr, _("Subentries control encoding error!\n") );
675 if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
679 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
680 c[i].ldctl_iscritical = subentries < 1;
685 #ifdef LDAP_CLIENT_UPDATE
687 if (( cuber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
691 if ( lcup_cookie.bv_len == 0 ) {
692 err = ber_printf( cuber, "{ei}", abs(lcup), lcup_cint );
694 err = ber_printf( cuber, "{ei{sO}}", abs(lcup), lcup_cint,
695 LDAP_CUP_COOKIE_OID, &lcup_cookie );
698 if ( err == LBER_ERROR ) {
699 ber_free( cuber, 1 );
700 fprintf( stderr, _("client update control encoding error!\n") );
704 if ( ber_flatten( cuber, &cubvalp ) == LBER_ERROR ) {
708 c[i].ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE;
709 c[i].ldctl_value = (*cubvalp);
710 c[i].ldctl_iscritical = lcup < 0;
717 if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
721 if ( sync_cookie.bv_len == 0 ) {
722 err = ber_printf( syncber, "{e}", abs(ldapsync) );
724 err = ber_printf( syncber, "{eO}", abs(ldapsync),
728 if ( err == LBER_ERROR ) {
729 ber_free( syncber, 1 );
730 fprintf( stderr, _("ldap sync control encoding error!\n") );
734 if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
738 c[i].ldctl_oid = LDAP_CONTROL_SYNC;
739 c[i].ldctl_value = (*syncbvalp);
740 c[i].ldctl_iscritical = ldapsync < 0;
745 if ( valuesReturnFilter ) {
746 if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
750 if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
751 ber_free( vrber, 1 );
752 fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
756 if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
760 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
761 c[i].ldctl_iscritical = valuesReturnFilter > 1;
765 #ifdef LDAP_CONTROL_PAGEDRESULTS
766 if ( pagedResults ) {
767 if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
771 ber_printf( prber, "{iO}", pageSize, &cookie );
772 if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == -1 ) {
776 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
777 c[i].ldctl_iscritical = pagedResults > 1;
782 tool_server_controls( ld, c, i );
784 ber_free( seber, 1 );
785 ber_free( vrber, 1 );
786 ber_free( prber, 1 );
790 fprintf( stderr, _("filter%s: %s\nrequesting: "),
791 infile != NULL ? _(" pattern") : "",
794 if ( attrs == NULL ) {
795 fprintf( stderr, _("ALL") );
797 for ( i = 0; attrs[ i ] != NULL; ++i ) {
798 fprintf( stderr, "%s ", attrs[ i ] );
801 fprintf( stderr, "\n" );
805 printf( _("# extended LDIF\n") );
806 } else if ( ldif < 3 ) {
807 printf( _("version: %d\n\n"), 1 );
812 printf(_("# LDAPv%d\n"), protocol);
813 printf(_("# base <%s> with scope %s\n"),
814 base ? base : "", (scope == LDAP_SCOPE_BASE) ? "base"
815 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"));
816 printf(_("# filter%s: %s\n"), infile != NULL ? _(" pattern") : "",
818 printf(_("# requesting: "));
820 if ( attrs == NULL ) {
823 for ( i = 0; attrs[ i ] != NULL; ++i ) {
824 printf( "%s ", attrs[ i ] );
829 printf(_("\n# with manageDSAit %scontrol"),
830 manageDSAit > 1 ? _("critical ") : "" );
833 printf(_("\n# with noop %scontrol"),
834 noop > 1 ? _("critical ") : "" );
837 printf(_("\n# with subentries %scontrol: %s"),
838 subentries < 0 ? _("critical ") : "",
839 abs(subentries) == 1 ? "false" : "true" );
841 if ( valuesReturnFilter ) {
842 printf(_("\n# with valuesReturnFilter %scontrol: %s"),
843 valuesReturnFilter > 1 ? _("critical ") : "", vrFilter );
845 #ifdef LDAP_CONTROL_PAGEDRESULTS
847 printf(_("\n# with pagedResults %scontrol: size=%d"),
848 (pagedResults > 1) ? _("critical ") : "",
853 printf( _("\n#\n\n") );
856 if ( infile == NULL ) {
857 rc = dosearch( ld, base, scope, NULL, filtpattern,
858 attrs, attrsonly, NULL, NULL, NULL, -1 );
863 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
864 line[ strlen( line ) - 1 ] = '\0';
870 rc = dosearch( ld, base, scope, filtpattern, line,
871 attrs, attrsonly, NULL, NULL, NULL, -1 );
878 #ifdef LDAP_CONTROL_PAGEDRESULTS
879 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
881 int i, moreEntries, tmpSize;
883 /* Loop to get the next pages when
884 * enter is pressed on the terminal.
886 if ( entriesLeft > 0 ) {
887 printf( _("Estimate entries: %d\n"), entriesLeft );
889 printf( _("Press [size] Enter for the next {%d|size} entries.\n"),
892 moreEntries = getchar();
893 while ( moreEntries != EOF && moreEntries != '\n' ) {
894 if ( i < (int)sizeof(buf) - 1 ) {
895 buf[i] = moreEntries;
898 moreEntries = getchar();
902 if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
903 int num = sscanf( buf, "%d", &tmpSize );
905 fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), buf);
909 pageSize = (ber_int_t)tmpSize;
918 ldap_pvt_tls_destroy();
931 LDAPControl **sctrls,
932 LDAPControl **cctrls,
933 struct timeval *timeout,
943 LDAPMessage *res, *msg;
947 struct berval *retdata = NULL;
948 int nresponses_psearch = -1;
949 int cancel_msgid = -1;
952 if( filtpatt != NULL ) {
953 filter = malloc( strlen( filtpatt ) + strlen( value ) );
954 if( filter == NULL ) {
959 sprintf( filter, filtpatt, value );
962 fprintf( stderr, _("filter: %s\n"), filter );
966 printf( _("#\n# filter: %s\n#\n"), filter );
977 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
978 sctrls, cctrls, timeout, sizelimit, &msgid );
980 if ( filtpatt != NULL ) {
984 if( rc != LDAP_SUCCESS ) {
985 fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
986 prog, ldap_err2string( rc ), rc );
990 nresponses = nentries = nreferences = nextended = npartial = 0;
994 while ((rc = ldap_result( ld, LDAP_RES_ANY,
995 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
999 (void) ldap_sort_entries( ld, &res,
1000 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1003 for ( msg = ldap_first_message( ld, res );
1005 msg = ldap_next_message( ld, msg ) )
1007 if ( nresponses++ ) putchar('\n');
1009 if ( nresponses_psearch >= 0 )
1010 nresponses_psearch++;
1013 switch( ldap_msgtype( msg ) ) {
1014 case LDAP_RES_SEARCH_ENTRY:
1016 print_entry( ld, msg, attrsonly );
1019 case LDAP_RES_SEARCH_REFERENCE:
1021 print_reference( ld, msg );
1024 case LDAP_RES_EXTENDED:
1026 print_extended( ld, msg );
1028 if( ldap_msgid( msg ) == 0 ) {
1029 /* unsolicited extended operation */
1034 if ( cancel_msgid != -1 &&
1035 cancel_msgid == ldap_msgid( msg ) ) {
1036 printf(_("Cancelled \n"));
1037 printf(_("cancel_msgid = %d\n"), cancel_msgid);
1044 case LDAP_RES_EXTENDED_PARTIAL:
1046 print_partial( ld, msg );
1049 case LDAP_RES_SEARCH_RESULT:
1050 rc = print_result( ld, msg, 1 );
1051 #ifdef LDAP_CONTROL_PAGEDRESULTS
1052 if ( pageSize != 0 ) {
1053 rc = parse_page_control( ld, msg, &cookie );
1057 #ifdef LDAP_CLIENT_UPDATE
1058 if ( lcup == LDAP_CUP_PERSIST_ONLY ||
1059 lcup == LDAP_CUP_SYNC_AND_PERSIST ) {
1063 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
1067 if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1075 case LDAP_RES_INTERMEDIATE_RESP:
1076 ldap_parse_intermediate_resp_result( ld, msg, &retoid, &retdata, 0 );
1077 nresponses_psearch = 0;
1079 if ( strcmp( retoid, LDAP_SYNC_INFO ) ) {
1080 printf(_("Unexpected Intermediate Response\n"));
1081 ldap_memfree( retoid );
1082 ber_bvfree( retdata );
1085 printf(_("SyncInfo Received\n"));
1086 ldap_memfree( retoid );
1087 ber_bvfree( retdata );
1093 #ifdef LDAP_CLIENT_UPDATE
1094 if ( lcup && lcup_slimit != -1 && nresponses >= lcup_slimit ) {
1095 ldap_abandon (ld, ldap_msgid(msg));
1100 if ( ldapsync && sync_slimit != -1 &&
1101 nresponses_psearch >= sync_slimit ) {
1102 BerElement *msgidber = NULL;
1103 struct berval *msgidvalp = NULL;
1104 msgidber = ber_alloc_t(LBER_USE_DER);
1105 ber_printf(msgidber, "{i}", msgid);
1106 ber_flatten(msgidber, &msgidvalp);
1107 ldap_extended_operation(ld, LDAP_EXOP_X_CANCEL,
1108 msgidvalp, NULL, NULL, &cancel_msgid);
1109 nresponses_psearch = -1;
1115 ldap_msgfree( res );
1119 ldap_perror( ld, "ldap_result" );
1124 ldap_msgfree( res );
1125 #ifdef LDAP_CONTROL_PAGEDRESULTS
1126 if ( pageSize != 0 ) {
1127 npagedresponses = npagedresponses + nresponses;
1128 npagedentries = npagedentries + nentries;
1129 npagedreferences = npagedreferences + nreferences;
1130 npagedextended = npagedextended + nextended;
1131 npagedpartial = npagedpartial + npartial;
1132 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
1133 printf( _("\n# numResponses: %d\n"), npagedresponses );
1134 if( nentries ) printf( _("# numEntries: %d\n"), npagedentries );
1135 if( nextended ) printf( _("# numExtended: %d\n"), npagedextended );
1136 if( npartial ) printf( _("# numPartial: %d\n"), npagedpartial );
1137 if( nreferences ) printf( _("# numReferences: %d\n"), npagedreferences );
1142 printf( _("\n# numResponses: %d\n"), nresponses );
1143 if( nentries ) printf( _("# numEntries: %d\n"), nentries );
1144 if( nextended ) printf( _("# numExtended: %d\n"), nextended );
1145 if( npartial ) printf( _("# numPartial: %d\n"), npartial );
1146 if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
1153 /* This is the original version, the old way of doing things. */
1161 char tmpfname[ 256 ];
1164 BerElement *ber = NULL;
1165 struct berval **bvals;
1166 LDAPControl **ctrls = NULL;
1169 dn = ldap_get_dn( ld, entry );
1173 ufn = ldap_dn2ufn( dn );
1174 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1176 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1178 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1180 if( rc != LDAP_SUCCESS ) {
1181 fprintf(stderr, _("print_entry: %d\n"), rc );
1182 ldap_perror( ld, "ldap_get_entry_controls" );
1183 exit( EXIT_FAILURE );
1187 print_ctrls( ctrls );
1188 ldap_controls_free( ctrls );
1193 ufn = ldap_dn2ufn( dn );
1195 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1198 if( ufn != NULL ) ldap_memfree( ufn );
1201 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1202 a = ldap_next_attribute( ld, entry, ber ) )
1205 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1207 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1208 for ( i = 0; bvals[i] != NULL; i++ ) {
1209 if ( vals2tmp > 1 || ( vals2tmp
1210 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1213 /* write value to file */
1214 snprintf( tmpfname, sizeof tmpfname,
1215 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1219 tmpfd = mkstemp( tmpfname );
1226 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1231 if ( fwrite( bvals[ i ]->bv_val,
1232 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1241 snprintf( url, sizeof url, "%s%s", urlpre,
1242 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1245 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1248 write_ldif( LDIF_PUT_VALUE, a,
1249 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1252 ber_bvecfree( bvals );
1262 /* This is the proposed new way of doing things.
1263 * It is more efficient, but the API is non-standard.
1272 char tmpfname[ 256 ];
1275 BerElement *ber = NULL;
1276 struct berval bv, *bvals, **bvp = &bvals;
1277 LDAPControl **ctrls = NULL;
1280 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1283 ufn = ldap_dn2ufn( bv.bv_val );
1284 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1286 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1288 rc = ldap_int_get_controls( ber, &ctrls );
1290 if( rc != LDAP_SUCCESS ) {
1291 fprintf(stderr, _("print_entry: %d\n"), rc );
1292 ldap_perror( ld, "ldap_get_entry_controls" );
1293 exit( EXIT_FAILURE );
1297 print_ctrls( ctrls );
1298 ldap_controls_free( ctrls );
1303 ufn = ldap_dn2ufn( bv.bv_val );
1305 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1308 if( ufn != NULL ) ldap_memfree( ufn );
1310 if ( attrsonly ) bvp = NULL;
1312 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1314 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1316 if (bv.bv_val == NULL) break;
1318 if ( attrsonly || !bvals ) {
1319 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1322 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1323 if ( vals2tmp > 1 || ( vals2tmp
1324 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1327 /* write value to file */
1328 snprintf( tmpfname, sizeof tmpfname,
1329 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1330 tmpdir, bv.bv_val );
1333 tmpfd = mkstemp( tmpfname );
1340 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1345 if ( fwrite( bvals[ i ].bv_val,
1346 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1355 snprintf( url, sizeof url, "%s%s", urlpre,
1356 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1359 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1362 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1363 bvals[ i ].bv_val, bvals[ i ].bv_len );
1366 ber_memfree( bvals );
1376 static void print_reference(
1378 LDAPMessage *reference )
1382 LDAPControl **ctrls;
1385 printf(_("# search reference\n"));
1388 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1390 if( rc != LDAP_SUCCESS ) {
1391 ldap_perror(ld, "ldap_parse_reference");
1392 exit( EXIT_FAILURE );
1397 for( i=0; refs[i] != NULL; i++ ) {
1398 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1399 "ref", refs[i], strlen(refs[i]) );
1401 ber_memvfree( (void **) refs );
1405 print_ctrls( ctrls );
1406 ldap_controls_free( ctrls );
1410 static void print_extended(
1412 LDAPMessage *extended )
1415 char *retoid = NULL;
1416 struct berval *retdata = NULL;
1419 printf(_("# extended result response\n"));
1422 rc = ldap_parse_extended_result( ld, extended,
1423 &retoid, &retdata, 0 );
1425 if( rc != LDAP_SUCCESS ) {
1426 ldap_perror(ld, "ldap_parse_extended_result");
1427 exit( EXIT_FAILURE );
1431 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1432 "extended", retoid, retoid ? strlen(retoid) : 0 );
1434 ber_memfree( retoid );
1438 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1439 "data", retdata->bv_val, retdata->bv_len );
1441 ber_bvfree( retdata );
1444 print_result( ld, extended, 0 );
1447 static void print_partial(
1449 LDAPMessage *partial )
1452 char *retoid = NULL;
1453 struct berval *retdata = NULL;
1454 LDAPControl **ctrls = NULL;
1457 printf(_("# extended partial response\n"));
1460 rc = ldap_parse_extended_partial( ld, partial,
1461 &retoid, &retdata, &ctrls, 0 );
1463 if( rc != LDAP_SUCCESS ) {
1464 ldap_perror(ld, "ldap_parse_extended_partial");
1465 exit( EXIT_FAILURE );
1469 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1470 "partial", retoid, retoid ? strlen(retoid) : 0 );
1473 ber_memfree( retoid );
1477 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1478 "data", retdata->bv_val, retdata->bv_len );
1481 ber_bvfree( retdata );
1485 print_ctrls( ctrls );
1486 ldap_controls_free( ctrls );
1490 static int print_result(
1492 LDAPMessage *result, int search )
1496 char *matcheddn = NULL;
1499 LDAPControl **ctrls = NULL;
1503 printf(_("# search result\n"));
1506 printf("%s: %d\n", _("search"), ldap_msgid(result) );
1510 rc = ldap_parse_result( ld, result,
1511 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1513 if( rc != LDAP_SUCCESS ) {
1514 ldap_perror(ld, "ldap_parse_result");
1515 exit( EXIT_FAILURE );
1520 printf( _("result: %d %s\n"), err, ldap_err2string(err) );
1522 } else if ( err != LDAP_SUCCESS ) {
1523 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1529 write_ldif( LDIF_PUT_VALUE,
1530 "matchedDN", matcheddn, strlen(matcheddn) );
1532 fprintf( stderr, _("Matched DN: %s\n"), matcheddn );
1536 ber_memfree( matcheddn );
1542 write_ldif( LDIF_PUT_TEXT, "text",
1543 text, strlen(text) );
1545 fprintf( stderr, _("Additional information: %s\n"), text );
1549 ber_memfree( text );
1554 for( i=0; refs[i] != NULL; i++ ) {
1556 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1558 fprintf( stderr, _("Referral: %s\n"), refs[i] );
1562 ber_memvfree( (void **) refs );
1566 print_ctrls( ctrls );
1567 ldap_controls_free( ctrls );
1573 static void print_ctrls(
1574 LDAPControl **ctrls )
1577 for(i=0; ctrls[i] != NULL; i++ ) {
1578 /* control: OID criticality base64value */
1579 struct berval *b64 = NULL;
1584 len += strlen( ctrls[i]->ldctl_oid );
1586 /* add enough for space after OID and the critical value itself */
1587 len += ctrls[i]->ldctl_iscritical
1588 ? sizeof("true") : sizeof("false");
1590 /* convert to base64 */
1591 if( ctrls[i]->ldctl_value.bv_len ) {
1592 b64 = ber_memalloc( sizeof(struct berval) );
1594 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1595 ctrls[i]->ldctl_value.bv_len ) + 1;
1596 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1598 b64->bv_len = lutil_b64_ntop(
1599 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1600 b64->bv_val, b64->bv_len );
1604 len += 1 + b64->bv_len;
1607 str = malloc( len + 1 );
1609 strcpy( str, ": " );
1613 strcat( str, ctrls[i]->ldctl_oid );
1614 strcat( str, ctrls[i]->ldctl_iscritical
1615 ? " true" : " false" );
1619 strcat(str, b64->bv_val );
1623 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1624 "control", str, len );
1633 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1637 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1641 fputs( ldif, stdout );
1642 ber_memfree( ldif );
1648 #ifdef LDAP_CONTROL_PAGEDRESULTS
1652 LDAPMessage *result,
1653 struct berval *cookie )
1657 LDAPControl **ctrl = NULL;
1658 LDAPControl *ctrlp = NULL;
1661 struct berval servercookie = { 0, NULL };
1663 rc = ldap_parse_result( ld, result,
1664 &err, NULL, NULL, NULL, &ctrl, 0 );
1666 if( rc != LDAP_SUCCESS ) {
1667 ldap_perror(ld, "ldap_parse_result");
1668 exit( EXIT_FAILURE );
1671 if ( err != LDAP_SUCCESS ) {
1672 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1676 /* Parse the control value
1677 * searchResult ::= SEQUENCE {
1678 * size INTEGER (0..maxInt),
1679 * -- result set size estimate from server - unused
1680 * cookie OCTET STRING
1684 ber = ber_init( &ctrlp->ldctl_value );
1685 if ( ber == NULL ) {
1686 fprintf( stderr, _("Internal error.\n") );
1687 return EXIT_FAILURE;
1690 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1691 ber_dupbv( cookie, &servercookie );
1692 (void) ber_free( ber, 1 );
1694 if( tag == LBER_ERROR ) {
1696 _("Paged results response control could not be decoded.\n") );
1697 return EXIT_FAILURE;
1700 if( entriesLeft < 0 ) {
1702 _("Invalid entries estimate in paged results response.\n") );
1703 return EXIT_FAILURE;
1706 ldap_controls_free( ctrl );
1709 morePagedResults = 0;