1 /* ldapsearch -- a tool for searching LDAP directories */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 1998-2001 Net Boolean Incorporated.
8 * Portions Copyright 2001-2003 IBM Corporation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that this notice is preserved and that due credit is given
24 * to the University of Michigan at Ann Arbor. The name of the
25 * University may not be used to endorse or promote products derived
26 * from this software without specific prior written permission. This
27 * software is provided ``as is'' without express or implied warranty.
30 * This work was originally developed by the University of Michigan
31 * (as part of U-MICH LDAP). Additional significant contributors
43 #include <ac/stdlib.h>
46 #include <ac/string.h>
47 #include <ac/unistd.h>
51 #include <ac/signal.h>
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
67 #include "lutil_ldap.h"
68 #include "ldap_defaults.h"
76 * NOTE: we use this deprecated function only because
77 * we want ldapsearch to provide some client-side sorting
81 typedef int (LDAP_SORT_AD_CMP_PROC) LDAP_P(( /* deprecated */
82 LDAP_CONST char *left,
83 LDAP_CONST char *right ));
85 LDAP_F( int ) /* deprecated */
86 ldap_sort_entries LDAP_P(( LDAP *ld,
88 LDAP_CONST char *attr,
89 LDAP_SORT_AD_CMP_PROC *cmp ));
92 static int scope = LDAP_SCOPE_SUBTREE;
93 static int deref = -1;
95 static int timelimit = -1;
96 static int sizelimit = -1;
98 static char *def_tmpdir;
99 static char *def_urlpre;
101 #if defined(__CYGWIN__) || defined(__MINGW32__)
102 /* Turn off commandline globbing, otherwise you cannot search for
111 fprintf( stderr, _("usage: %s [options] [filter [attributes...]]\nwhere:\n"), prog);
112 fprintf( stderr, _(" filter\tRFC-2254 compliant LDAP search filter\n"));
113 fprintf( stderr, _(" attributes\twhitespace-separated list of attribute descriptions\n"));
114 fprintf( stderr, _(" which may include:\n"));
115 fprintf( stderr, _(" 1.1 no attributes\n"));
116 fprintf( stderr, _(" * all user attributes\n"));
117 fprintf( stderr, _(" + all operational attributes\n"));
120 fprintf( stderr, _("Search options:\n"));
121 fprintf( stderr, _(" -a deref one of never (default), always, search, or find\n"));
122 fprintf( stderr, _(" -A retrieve attribute names only (no values)\n"));
123 fprintf( stderr, _(" -b basedn base dn for search\n"));
124 fprintf( stderr, _(" -E [!]<ext>[=<extparam>] search extensions (! indicates criticality)\n"));
125 #ifdef LDAP_CONTROL_DONTUSECOPY
126 fprintf( stderr, _(" !dontUseCopy (Don't Use Copy)\n"));
128 fprintf( stderr, _(" [!]domainScope (domain scope)\n"));
129 fprintf( stderr, _(" [!]mv=<filter> (matched values filter)\n"));
130 fprintf( stderr, _(" [!]pr=<size>[/prompt|noprompt] (paged results/prompt)\n"));
131 fprintf( stderr, _(" [!]subentries[=true|false] (subentries)\n"));
132 fprintf( stderr, _(" [!]sync=ro[/<cookie>] (LDAP Sync refreshOnly)\n"));
133 fprintf( stderr, _(" rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
134 fprintf( stderr, _(" [!]<oid>=:<value> (generic control; no response handling)\n"));
135 fprintf( stderr, _(" -F prefix URL prefix for files (default: %s)\n"), def_urlpre);
136 fprintf( stderr, _(" -l limit time limit (in seconds, or \"none\" or \"max\") for search\n"));
137 fprintf( stderr, _(" -L print responses in LDIFv1 format\n"));
138 fprintf( stderr, _(" -LL print responses in LDIF format without comments\n"));
139 fprintf( stderr, _(" -LLL print responses in LDIF format without comments\n"));
140 fprintf( stderr, _(" and version\n"));
141 fprintf( stderr, _(" -s scope one of base, one, sub or children (search scope)\n"));
142 fprintf( stderr, _(" -S attr sort the results by attribute `attr'\n"));
143 fprintf( stderr, _(" -t write binary values to files in temporary directory\n"));
144 fprintf( stderr, _(" -tt write all values to files in temporary directory\n"));
145 fprintf( stderr, _(" -T path write files to directory specified by path (default: %s)\n"), def_tmpdir);
146 fprintf( stderr, _(" -u include User Friendly entry names in the output\n"));
147 fprintf( stderr, _(" -z limit size limit (in entries, or \"none\" or \"max\") for search\n"));
149 exit( EXIT_FAILURE );
152 static void print_entry LDAP_P((
157 static void print_reference(
159 LDAPMessage *reference );
161 static void print_extended(
163 LDAPMessage *extended );
165 static void print_partial(
167 LDAPMessage *partial );
169 static int print_result(
174 static int dosearch LDAP_P((
182 LDAPControl **sctrls,
183 LDAPControl **cctrls,
184 struct timeval *timeout,
187 static char *tmpdir = NULL;
188 static char *urlpre = NULL;
189 static char *base = NULL;
190 static char *sortattr = NULL;
191 static int includeufn, vals2tmp = 0;
193 static int subentries = 0, valuesReturnFilter = 0;
194 static char *vrFilter = NULL;
196 #ifdef LDAP_CONTROL_DONTUSECOPY
197 static int dontUseCopy = 0;
200 static int domainScope = 0;
202 static int ldapsync = 0;
203 static struct berval sync_cookie = { 0, NULL };
204 static int sync_slimit = -1;
206 /* cookie and morePagedResults moved to common.c */
207 static int pagedResults = 0;
208 static int pagePrompt = 1;
209 static ber_int_t pageSize = 0;
210 static ber_int_t entriesLeft = 0;
211 static int npagedresponses;
212 static int npagedentries;
213 static int npagedreferences;
214 static int npagedextended;
215 static int npagedpartial;
217 static LDAPControl *c = NULL;
218 static int nctrls = 0;
219 static int save_nctrls = 0;
227 tmpc = realloc( c, sizeof( LDAPControl ) * nctrls );
228 if ( tmpc == NULL ) {
231 _("unable to make room for control; out of memory?\n"));
244 if (*LDAP_DIRSEP != '/') {
245 for (p = url; *p; p++) {
246 if (*p == *LDAP_DIRSEP)
253 const char options[] = "a:Ab:cE:F:l:Ls:S:tT:uz:"
254 "Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
257 handle_private_option( int i )
260 char *control, *cvalue, *next;
262 case 'a': /* set alias deref option */
263 if ( strcasecmp( optarg, "never" ) == 0 ) {
264 deref = LDAP_DEREF_NEVER;
265 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
266 deref = LDAP_DEREF_SEARCHING;
267 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
268 deref = LDAP_DEREF_FINDING;
269 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
270 deref = LDAP_DEREF_ALWAYS;
273 _("alias deref should be never, search, find, or always\n") );
277 case 'A': /* retrieve attribute names only -- no values */
280 case 'b': /* search base */
281 base = ber_strdup( optarg );
283 case 'E': /* search extensions */
284 if( protocol == LDAP_VERSION2 ) {
285 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
287 exit( EXIT_FAILURE );
290 /* should be extended to support comma separated list of
291 * [!]key[=value] parameters, e.g. -E !foo,bar=567
296 if( optarg[0] == '!' ) {
301 control = ber_strdup( optarg );
302 if ( (cvalue = strchr( control, '=' )) != NULL ) {
306 if ( strcasecmp( control, "mv" ) == 0 ) {
307 /* ValuesReturnFilter control */
308 if( valuesReturnFilter ) {
310 _("ValuesReturnFilter previously specified\n"));
311 exit( EXIT_FAILURE );
313 valuesReturnFilter= 1 + crit;
315 if ( cvalue == NULL ) {
317 _("missing filter in ValuesReturnFilter control\n"));
318 exit( EXIT_FAILURE );
322 protocol = LDAP_VERSION3;
324 } else if ( strcasecmp( control, "pr" ) == 0 ) {
326 /* PagedResults control */
327 if ( pagedResults != 0 ) {
329 _("PagedResultsControl previously specified\n") );
330 exit( EXIT_FAILURE );
333 if( cvalue != NULL ) {
336 promptp = strchr( cvalue, '/' );
337 if ( promptp != NULL ) {
339 if ( strcasecmp( promptp, "prompt" ) == 0 ) {
341 } else if ( strcasecmp( promptp, "noprompt" ) == 0) {
345 _("Invalid value for PagedResultsControl,"
346 " %s/%s.\n"), cvalue, promptp );
347 exit( EXIT_FAILURE );
350 num = sscanf( cvalue, "%d", &tmp );
353 _("Invalid value for PagedResultsControl, %s.\n"),
355 exit( EXIT_FAILURE );
358 fprintf(stderr, _("Invalid value for PagedResultsControl.\n"));
359 exit( EXIT_FAILURE );
361 pageSize = (ber_int_t) tmp;
362 pagedResults = 1 + crit;
364 #ifdef LDAP_CONTROL_DONTUSECOPY
365 } else if ( strcasecmp( control, "dontUseCopy" ) == 0 ) {
368 _("dontUseCopy control previously specified\n"));
369 exit( EXIT_FAILURE );
371 if( cvalue != NULL ) {
373 _("dontUseCopy: no control value expected\n") );
378 _("dontUseCopy: critical flag required\n") );
382 dontUseCopy = 1 + crit;
384 } else if ( strcasecmp( control, "domainScope" ) == 0 ) {
387 _("domainScope control previously specified\n"));
388 exit( EXIT_FAILURE );
390 if( cvalue != NULL ) {
392 _("domainScope: no control value expected\n") );
396 domainScope = 1 + crit;
398 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
401 _("subentries control previously specified\n"));
402 exit( EXIT_FAILURE );
404 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
406 } else if ( strcasecmp( cvalue, "false") == 0 ) {
410 _("subentries control value \"%s\" invalid\n"),
412 exit( EXIT_FAILURE );
414 if( crit ) subentries *= -1;
416 } else if ( strcasecmp( control, "sync" ) == 0 ) {
420 fprintf( stderr, _("sync control previously specified\n") );
421 exit( EXIT_FAILURE );
423 if ( cvalue == NULL ) {
424 fprintf( stderr, _("missing specification of sync control\n"));
425 exit( EXIT_FAILURE );
427 if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
428 ldapsync = LDAP_SYNC_REFRESH_ONLY;
429 cookiep = strchr( cvalue, '/' );
430 if ( cookiep != NULL ) {
432 if ( *cookiep != '\0' ) {
433 ber_str2bv( cookiep, 0, 0, &sync_cookie );
436 } else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
437 ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
438 cookiep = strchr( cvalue, '/' );
439 if ( cookiep != NULL ) {
443 slimitp = strchr( cvalue, '/' );
444 if ( slimitp != NULL ) {
447 if ( cookiep != NULL && *cookiep != '\0' )
448 ber_str2bv( cookiep, 0, 0, &sync_cookie );
449 if ( slimitp != NULL && *slimitp != '\0' ) {
450 ival = strtol( slimitp, &next, 10 );
451 if ( next == NULL || next[0] != '\0' ) {
452 fprintf( stderr, _("Unable to parse sync control value \"%s\"\n"), slimitp );
453 exit( EXIT_FAILURE );
458 fprintf( stderr, _("sync control value \"%s\" invalid\n"),
460 exit( EXIT_FAILURE );
462 if ( crit ) ldapsync *= -1;
464 } else if ( tool_is_oid( control ) ) {
466 exit( EXIT_FAILURE );
470 c[ nctrls - 1 ].ldctl_oid = control;
473 if ( cvalue == NULL ) {
474 c[ nctrls - 1 ].ldctl_value.bv_val = NULL;
475 c[ nctrls - 1 ].ldctl_value.bv_len = 0;
477 } else if ( cvalue[ 0 ] == ':' ) {
485 * to use ldif_parse_line2() */
487 ldif_parse_line2( &cvalue[ -2 ], &type,
492 c[ nctrls - 1 ].ldctl_value = value;
495 ber_dupbv( &c[ nctrls - 1 ].ldctl_value, &value );
500 c[ nctrls - 1 ].ldctl_iscritical = crit;
503 fprintf( stderr, _("Invalid search extension name: %s\n"),
508 case 'F': /* uri prefix */
509 if( urlpre ) free( urlpre );
510 urlpre = strdup( optarg );
512 case 'l': /* time limit */
513 if ( strcasecmp( optarg, "none" ) == 0 ) {
516 } else if ( strcasecmp( optarg, "max" ) == 0 ) {
517 timelimit = LDAP_MAXINT;
520 ival = strtol( optarg, &next, 10 );
521 if ( next == NULL || next[0] != '\0' ) {
523 _("Unable to parse time limit \"%s\"\n"), optarg );
524 exit( EXIT_FAILURE );
528 if( timelimit < 0 || timelimit > LDAP_MAXINT ) {
529 fprintf( stderr, _("%s: invalid timelimit (%d) specified\n"),
531 exit( EXIT_FAILURE );
534 case 'L': /* print entries in LDIF format */
537 case 's': /* search scope */
538 if ( strncasecmp( optarg, "base", sizeof("base")-1 ) == 0 ) {
539 scope = LDAP_SCOPE_BASE;
540 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
541 scope = LDAP_SCOPE_ONELEVEL;
542 } else if (( strcasecmp( optarg, "subordinate" ) == 0 )
543 || ( strcasecmp( optarg, "children" ) == 0 ))
545 scope = LDAP_SCOPE_SUBORDINATE;
546 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
547 scope = LDAP_SCOPE_SUBTREE;
549 fprintf( stderr, _("scope should be base, one, or sub\n") );
553 case 'S': /* sort attribute */
554 sortattr = strdup( optarg );
556 case 't': /* write attribute values to TMPDIR files */
559 case 'T': /* tmpdir */
560 if( tmpdir ) free( tmpdir );
561 tmpdir = strdup( optarg );
563 case 'u': /* include UFN */
566 case 'z': /* size limit */
567 if ( strcasecmp( optarg, "none" ) == 0 ) {
570 } else if ( strcasecmp( optarg, "max" ) == 0 ) {
571 sizelimit = LDAP_MAXINT;
574 ival = strtol( optarg, &next, 10 );
575 if ( next == NULL || next[0] != '\0' ) {
577 _("Unable to parse size limit \"%s\"\n"), optarg );
578 exit( EXIT_FAILURE );
582 if( sizelimit < 0 || sizelimit > LDAP_MAXINT ) {
583 fprintf( stderr, _("%s: invalid sizelimit (%d) specified\n"),
585 exit( EXIT_FAILURE );
596 private_conn_setup( LDAP *ld )
599 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref )
600 != LDAP_OPT_SUCCESS )
602 fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
603 exit( EXIT_FAILURE );
606 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit )
607 != LDAP_OPT_SUCCESS )
610 _("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
611 exit( EXIT_FAILURE );
614 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit )
615 != LDAP_OPT_SUCCESS )
618 _("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
619 exit( EXIT_FAILURE );
624 main( int argc, char **argv )
626 char *filtpattern, **attrs = NULL, line[BUFSIZ];
630 BerElement *seber = NULL, *vrber = NULL;
632 BerElement *syncber = NULL;
633 struct berval *syncbvalp = NULL;
636 tool_init( TOOL_SEARCH );
638 npagedresponses = npagedentries = npagedreferences =
639 npagedextended = npagedpartial = 0;
641 prog = lutil_progname( "ldapsearch", argc, argv );
643 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
644 (def_tmpdir = getenv("TMP")) == NULL &&
645 (def_tmpdir = getenv("TEMP")) == NULL )
647 def_tmpdir = LDAP_TMPDIR;
651 def_tmpdir = LDAP_TMPDIR;
653 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
655 if( def_urlpre == NULL ) {
660 sprintf( def_urlpre, "file:///%s/",
661 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
663 urlize( def_urlpre );
665 tool_args( argc, argv );
667 if (( argc - optind < 1 ) ||
668 ( *argv[optind] != '(' /*')'*/ &&
669 ( strchr( argv[optind], '=' ) == NULL ) ) )
671 filtpattern = "(objectclass=*)";
673 filtpattern = argv[optind++];
676 if ( argv[optind] != NULL ) {
677 attrs = &argv[optind];
680 if ( infile != NULL ) {
681 if ( infile[0] == '-' && infile[1] == '\0' ) {
683 } else if (( fp = fopen( infile, "r" )) == NULL ) {
689 if ( tmpdir == NULL ) {
692 if ( urlpre == NULL )
696 if( urlpre == NULL ) {
697 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
699 if( urlpre == NULL ) {
704 sprintf( urlpre, "file:///%s/",
705 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
713 ld = tool_conn_setup( 0, &private_conn_setup );
715 if ( pw_file || want_bindpw ) {
717 rc = lutil_get_filed_password( pw_file, &passwd );
718 if( rc ) return EXIT_FAILURE;
720 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
721 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
728 save_nctrls = nctrls;
731 #ifdef LDAP_CONTROL_DONTUSECOPY
738 || valuesReturnFilter )
741 #ifdef LDAP_CONTROL_DONTUSECOPY
747 c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
748 c[i].ldctl_value.bv_val = NULL;
749 c[i].ldctl_value.bv_len = 0;
750 c[i].ldctl_iscritical = dontUseCopy > 1;
760 c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
761 c[i].ldctl_value.bv_val = NULL;
762 c[i].ldctl_value.bv_len = 0;
763 c[i].ldctl_iscritical = domainScope > 1;
772 if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
776 err = ber_printf( seber, "b", abs(subentries) == 1 ? 0 : 1 );
778 ber_free( seber, 1 );
779 fprintf( stderr, _("Subentries control encoding error!\n") );
783 if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
787 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
788 c[i].ldctl_iscritical = subentries < 1;
797 if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
801 if ( sync_cookie.bv_len == 0 ) {
802 err = ber_printf( syncber, "{e}", abs(ldapsync) );
804 err = ber_printf( syncber, "{eO}", abs(ldapsync),
808 if ( err == LBER_ERROR ) {
809 ber_free( syncber, 1 );
810 fprintf( stderr, _("ldap sync control encoding error!\n") );
814 if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
818 c[i].ldctl_oid = LDAP_CONTROL_SYNC;
819 c[i].ldctl_value = (*syncbvalp);
820 c[i].ldctl_iscritical = ldapsync < 0;
824 if ( valuesReturnFilter ) {
829 if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
833 if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
834 ber_free( vrber, 1 );
835 fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
839 if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
843 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
844 c[i].ldctl_iscritical = valuesReturnFilter > 1;
848 if ( pagedResults ) {
853 if ( ldap_create_page_control_value( ld,
854 pageSize, &pr_cookie, &c[i].ldctl_value ) )
859 if ( pr_cookie.bv_val != NULL ) {
860 ber_memfree( pr_cookie.bv_val );
861 pr_cookie.bv_val = NULL;
862 pr_cookie.bv_len = 0;
865 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
866 c[i].ldctl_iscritical = pagedResults > 1;
871 tool_server_controls( ld, c, i );
873 ber_free( seber, 1 );
874 ber_free( vrber, 1 );
876 /* step back to the original number of controls, so that
877 * those set while parsing args are preserved */
878 nctrls = save_nctrls;
881 fprintf( stderr, _("filter%s: %s\nrequesting: "),
882 infile != NULL ? _(" pattern") : "",
885 if ( attrs == NULL ) {
886 fprintf( stderr, _("All userApplication attributes") );
888 for ( i = 0; attrs[ i ] != NULL; ++i ) {
889 fprintf( stderr, "%s ", attrs[ i ] );
892 fprintf( stderr, "\n" );
896 printf( _("# extended LDIF\n") );
897 } else if ( ldif < 3 ) {
898 printf( _("version: %d\n\n"), 1 );
902 char *realbase = base;
904 if ( realbase == NULL ) {
905 ldap_get_option( ld, LDAP_OPT_DEFBASE, (void **)&realbase );
909 printf(_("# LDAPv%d\n"), protocol);
910 printf(_("# base <%s>%s with scope %s\n"),
911 realbase ? realbase : "",
912 ( realbase == NULL || realbase != base ) ? " (default)" : "",
913 ((scope == LDAP_SCOPE_BASE) ? "baseObject"
914 : ((scope == LDAP_SCOPE_ONELEVEL) ? "oneLevel"
915 : ((scope == LDAP_SCOPE_SUBORDINATE) ? "children"
917 printf(_("# filter%s: %s\n"), infile != NULL ? _(" pattern") : "",
919 printf(_("# requesting: "));
921 if ( attrs == NULL ) {
924 for ( i = 0; attrs[ i ] != NULL; ++i ) {
925 printf( "%s ", attrs[ i ] );
930 printf(_("\n# with manageDSAit %scontrol"),
931 manageDSAit > 1 ? _("critical ") : "" );
934 printf(_("\n# with noop %scontrol"),
935 noop > 1 ? _("critical ") : "" );
938 printf(_("\n# with subentries %scontrol: %s"),
939 subentries < 0 ? _("critical ") : "",
940 abs(subentries) == 1 ? "false" : "true" );
942 if ( valuesReturnFilter ) {
943 printf(_("\n# with valuesReturnFilter %scontrol: %s"),
944 valuesReturnFilter > 1 ? _("critical ") : "", vrFilter );
946 if ( pagedResults ) {
947 printf(_("\n# with pagedResults %scontrol: size=%d"),
948 (pagedResults > 1) ? _("critical ") : "",
952 printf( _("\n#\n\n") );
954 if ( realbase && realbase != base ) {
955 ldap_memfree( realbase );
959 if ( infile == NULL ) {
960 rc = dosearch( ld, base, scope, NULL, filtpattern,
961 attrs, attrsonly, NULL, NULL, NULL, -1 );
966 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
967 line[ strlen( line ) - 1 ] = '\0';
973 rc = dosearch( ld, base, scope, filtpattern, line,
974 attrs, attrsonly, NULL, NULL, NULL, -1 );
981 if (( rc == LDAP_SUCCESS ) && pageSize && pr_morePagedResults ) {
983 int i, moreEntries, tmpSize;
985 /* Loop to get the next pages when
986 * enter is pressed on the terminal.
988 if ( pagePrompt != 0 ) {
989 if ( entriesLeft > 0 ) {
990 printf( _("Estimate entries: %d\n"), entriesLeft );
992 printf( _("Press [size] Enter for the next {%d|size} entries.\n"),
995 moreEntries = getchar();
996 while ( moreEntries != EOF && moreEntries != '\n' ) {
997 if ( i < (int)sizeof(buf) - 1 ) {
998 buf[i] = moreEntries;
1001 moreEntries = getchar();
1005 if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
1006 int num = sscanf( buf, "%d", &tmpSize );
1009 _("Invalid value for PagedResultsControl, %s.\n"), buf);
1010 return EXIT_FAILURE;
1013 pageSize = (ber_int_t)tmpSize;
1024 for ( ; save_nctrls-- > 0; ) {
1025 ber_memfree( c[ save_nctrls ].ldctl_value.bv_val );
1035 static int dosearch(
1043 LDAPControl **sctrls,
1044 LDAPControl **cctrls,
1045 struct timeval *timeout,
1055 LDAPMessage *res, *msg;
1057 char *retoid = NULL;
1058 struct berval *retdata = NULL;
1059 int nresponses_psearch = -1;
1060 int cancel_msgid = -1;
1062 if( filtpatt != NULL ) {
1063 size_t max_fsize = strlen( filtpatt ) + strlen( value ) + 1;
1064 filter = malloc( max_fsize );
1065 if( filter == NULL ) {
1067 return EXIT_FAILURE;
1070 if( snprintf( filter, max_fsize, filtpatt, value ) >= max_fsize ) {
1071 fprintf( stderr, "Bad filter pattern: \"%s\"\n", filtpatt );
1073 return EXIT_FAILURE;
1077 fprintf( stderr, _("filter: %s\n"), filter );
1081 printf( _("#\n# filter: %s\n#\n"), filter );
1089 if ( filtpatt != NULL ) {
1092 return LDAP_SUCCESS;
1095 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1096 sctrls, cctrls, timeout, sizelimit, &msgid );
1098 if ( filtpatt != NULL ) {
1102 if( rc != LDAP_SUCCESS ) {
1103 fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
1104 prog, ldap_err2string( rc ), rc );
1108 nresponses = nentries = nreferences = nextended = npartial = 0;
1112 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1113 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1116 rc = tool_check_abandon( ld, msgid );
1122 (void) ldap_sort_entries( ld, &res,
1123 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1126 for ( msg = ldap_first_message( ld, res );
1128 msg = ldap_next_message( ld, msg ) )
1130 if ( nresponses++ ) putchar('\n');
1131 if ( nresponses_psearch >= 0 )
1132 nresponses_psearch++;
1134 switch( ldap_msgtype( msg ) ) {
1135 case LDAP_RES_SEARCH_ENTRY:
1137 print_entry( ld, msg, attrsonly );
1140 case LDAP_RES_SEARCH_REFERENCE:
1142 print_reference( ld, msg );
1145 case LDAP_RES_EXTENDED:
1147 print_extended( ld, msg );
1149 if( ldap_msgid( msg ) == 0 ) {
1150 /* unsolicited extended operation */
1154 if ( cancel_msgid != -1 &&
1155 cancel_msgid == ldap_msgid( msg ) ) {
1156 printf(_("Cancelled \n"));
1157 printf(_("cancel_msgid = %d\n"), cancel_msgid);
1162 case LDAP_RES_SEARCH_RESULT:
1163 /* pagedResults stuff is dealt with
1164 * in tool_print_ctrls(), called by
1165 * print_results(). */
1166 rc = print_result( ld, msg, 1 );
1167 if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1173 case LDAP_RES_INTERMEDIATE:
1175 ldap_parse_intermediate( ld, msg,
1176 &retoid, &retdata, NULL, 0 );
1178 nresponses_psearch = 0;
1180 if ( strcmp( retoid, LDAP_SYNC_INFO ) == 0 ) {
1181 printf(_("SyncInfo Received\n"));
1182 ldap_memfree( retoid );
1183 ber_bvfree( retdata );
1187 print_partial( ld, msg );
1188 ldap_memfree( retoid );
1189 ber_bvfree( retdata );
1193 if ( ldapsync && sync_slimit != -1 &&
1194 nresponses_psearch >= sync_slimit ) {
1195 BerElement *msgidber = NULL;
1196 struct berval *msgidvalp = NULL;
1197 msgidber = ber_alloc_t(LBER_USE_DER);
1198 ber_printf(msgidber, "{i}", msgid);
1199 ber_flatten(msgidber, &msgidvalp);
1200 ldap_extended_operation(ld, LDAP_EXOP_CANCEL,
1201 msgidvalp, NULL, NULL, &cancel_msgid);
1202 nresponses_psearch = -1;
1206 ldap_msgfree( res );
1211 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
1215 ldap_msgfree( res );
1217 if ( pagedResults ) {
1218 npagedresponses += nresponses;
1219 npagedentries += nentries;
1220 npagedextended += nextended;
1221 npagedpartial += npartial;
1222 npagedreferences += nreferences;
1223 if ( ( pr_morePagedResults == 0 ) && ( ldif < 2 ) ) {
1224 printf( _("\n# numResponses: %d\n"), npagedresponses );
1225 if( npagedentries ) {
1226 printf( _("# numEntries: %d\n"), npagedentries );
1228 if( npagedextended ) {
1229 printf( _("# numExtended: %d\n"), npagedextended );
1231 if( npagedpartial ) {
1232 printf( _("# numPartial: %d\n"), npagedpartial );
1234 if( npagedreferences ) {
1235 printf( _("# numReferences: %d\n"), npagedreferences );
1238 } else if ( ldif < 2 ) {
1239 printf( _("\n# numResponses: %d\n"), nresponses );
1240 if( nentries ) printf( _("# numEntries: %d\n"), nentries );
1241 if( nextended ) printf( _("# numExtended: %d\n"), nextended );
1242 if( npartial ) printf( _("# numPartial: %d\n"), npartial );
1243 if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
1249 /* This is the proposed new way of doing things.
1250 * It is more efficient, but the API is non-standard.
1259 char tmpfname[ 256 ];
1262 BerElement *ber = NULL;
1263 struct berval bv, *bvals, **bvp = &bvals;
1264 LDAPControl **ctrls = NULL;
1267 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1270 ufn = ldap_dn2ufn( bv.bv_val );
1271 tool_write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1273 tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1275 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1276 if( rc != LDAP_SUCCESS ) {
1277 fprintf(stderr, _("print_entry: %d\n"), rc );
1278 tool_perror( "ldap_get_entry_controls", rc, NULL, NULL, NULL, NULL );
1279 exit( EXIT_FAILURE );
1283 tool_print_ctrls( ld, ctrls );
1284 ldap_controls_free( ctrls );
1289 ufn = ldap_dn2ufn( bv.bv_val );
1291 tool_write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1294 if( ufn != NULL ) ldap_memfree( ufn );
1296 if ( attrsonly ) bvp = NULL;
1298 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1300 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1302 if (bv.bv_val == NULL) break;
1305 tool_write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1307 } else if ( bvals ) {
1308 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1309 if ( vals2tmp > 1 || ( vals2tmp &&
1310 ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len )))
1313 /* write value to file */
1314 snprintf( tmpfname, sizeof tmpfname,
1315 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1316 tmpdir, bv.bv_val );
1319 tmpfd = mkstemp( tmpfname );
1326 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1331 if ( fwrite( bvals[ i ].bv_val,
1332 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1341 snprintf( url, sizeof url, "%s%s", urlpre,
1342 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1345 tool_write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1348 tool_write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1349 bvals[ i ].bv_val, bvals[ i ].bv_len );
1352 ber_memfree( bvals );
1361 static void print_reference(
1363 LDAPMessage *reference )
1367 LDAPControl **ctrls;
1370 printf(_("# search reference\n"));
1373 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1375 if( rc != LDAP_SUCCESS ) {
1376 tool_perror( "ldap_parse_reference", rc, NULL, NULL, NULL, NULL );
1377 exit( EXIT_FAILURE );
1382 for( i=0; refs[i] != NULL; i++ ) {
1383 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1384 "ref", refs[i], strlen(refs[i]) );
1386 ber_memvfree( (void **) refs );
1390 tool_print_ctrls( ld, ctrls );
1391 ldap_controls_free( ctrls );
1395 static void print_extended(
1397 LDAPMessage *extended )
1400 char *retoid = NULL;
1401 struct berval *retdata = NULL;
1404 printf(_("# extended result response\n"));
1407 rc = ldap_parse_extended_result( ld, extended,
1408 &retoid, &retdata, 0 );
1410 if( rc != LDAP_SUCCESS ) {
1411 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
1412 exit( EXIT_FAILURE );
1416 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1417 "extended", retoid, retoid ? strlen(retoid) : 0 );
1419 ber_memfree( retoid );
1423 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1424 "data", retdata->bv_val, retdata->bv_len );
1426 ber_bvfree( retdata );
1429 print_result( ld, extended, 0 );
1432 static void print_partial(
1434 LDAPMessage *partial )
1437 char *retoid = NULL;
1438 struct berval *retdata = NULL;
1439 LDAPControl **ctrls = NULL;
1442 printf(_("# extended partial response\n"));
1445 rc = ldap_parse_intermediate( ld, partial,
1446 &retoid, &retdata, &ctrls, 0 );
1448 if( rc != LDAP_SUCCESS ) {
1449 tool_perror( "ldap_parse_intermediate", rc, NULL, NULL, NULL, NULL );
1450 exit( EXIT_FAILURE );
1454 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1455 "partial", retoid, retoid ? strlen(retoid) : 0 );
1458 ber_memfree( retoid );
1462 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1463 "data", retdata->bv_val, retdata->bv_len );
1466 ber_bvfree( retdata );
1470 tool_print_ctrls( ld, ctrls );
1471 ldap_controls_free( ctrls );
1475 static int print_result(
1477 LDAPMessage *result, int search )
1481 char *matcheddn = NULL;
1484 LDAPControl **ctrls = NULL;
1488 printf(_("# search result\n"));
1491 printf("%s: %d\n", _("search"), ldap_msgid(result) );
1495 rc = ldap_parse_result( ld, result,
1496 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1498 if( rc != LDAP_SUCCESS ) {
1499 tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
1500 exit( EXIT_FAILURE );
1505 printf( _("result: %d %s\n"), err, ldap_err2string(err) );
1507 } else if ( err != LDAP_SUCCESS ) {
1508 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1514 tool_write_ldif( LDIF_PUT_VALUE,
1515 "matchedDN", matcheddn, strlen(matcheddn) );
1517 fprintf( stderr, _("Matched DN: %s\n"), matcheddn );
1521 ber_memfree( matcheddn );
1527 tool_write_ldif( LDIF_PUT_TEXT, "text",
1528 text, strlen(text) );
1530 fprintf( stderr, _("Additional information: %s\n"), text );
1534 ber_memfree( text );
1539 for( i=0; refs[i] != NULL; i++ ) {
1541 tool_write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1543 fprintf( stderr, _("Referral: %s\n"), refs[i] );
1547 ber_memvfree( (void **) refs );
1550 pr_morePagedResults = 0;
1553 tool_print_ctrls( ld, ctrls );
1554 ldap_controls_free( ctrls );