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"
41 static int scope = LDAP_SCOPE_SUBTREE;
42 static int deref = -1;
44 static int timelimit = -1;
45 static int sizelimit = -1;
47 static char *def_tmpdir;
48 static char *def_urlpre;
54 fprintf( stderr, _("usage: %s [options] [filter [attributes...]]\nwhere:\n"), prog);
55 fprintf( stderr, _(" filter\tRFC-2254 compliant LDAP search filter\n"));
56 fprintf( stderr, _(" attributes\twhitespace-separated list of attribute descriptions\n"));
57 fprintf( stderr, _(" which may include:\n"));
58 fprintf( stderr, _(" 1.1 no attributes\n"));
59 fprintf( stderr, _(" * all user attributes\n"));
60 fprintf( stderr, _(" + all operational attributes\n"));
63 fprintf( stderr, _("Search options:\n"));
64 fprintf( stderr, _(" -a deref one of never (default), always, search, or find\n"));
65 fprintf( stderr, _(" -A retrieve attribute names only (no values)\n"));
66 fprintf( stderr, _(" -b basedn base dn for search\n"));
67 fprintf( stderr, _(" -E [!]<ctrl>[=<ctrlparam>] search controls (! indicates criticality)\n"));
68 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
69 fprintf( stderr, _(" [!]domainScope (domain scope)\n"));
71 fprintf( stderr, _(" [!]mv=<filter> (matched values filter)\n"));
72 #ifdef LDAP_CONTROL_PAGEDRESULTS
73 fprintf( stderr, _(" [!]pr=<size> (paged results)\n"));
75 #ifdef LDAP_CONTROL_SUBENTRIES
76 fprintf( stderr, _(" [!]subentries[=true|false] (subentries)\n"));
79 fprintf( stderr, _(" [!]sync=ro[/<cookie>] (LDAP Sync refreshOnly)\n"));
80 fprintf( stderr, _(" rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
82 fprintf( stderr, _(" -F prefix URL prefix for files (default: %s)\n"), def_urlpre);
83 fprintf( stderr, _(" -l limit time limit (in seconds) for search\n"));
84 fprintf( stderr, _(" -L print responses in LDIFv1 format\n"));
85 fprintf( stderr, _(" -LL print responses in LDIF format without comments\n"));
86 fprintf( stderr, _(" -LLL print responses in LDIF format without comments\n"));
87 fprintf( stderr, _(" and version\n"));
88 fprintf( stderr, _(" -s scope one of base, one, or sub (search scope)\n"));
89 fprintf( stderr, _(" -S attr sort the results by attribute `attr'\n"));
90 fprintf( stderr, _(" -t write binary values to files in temporary directory\n"));
91 fprintf( stderr, _(" -tt write all values to files in temporary directory\n"));
92 fprintf( stderr, _(" -T path write files to directory specified by path (default: %s)\n"), def_tmpdir);
93 fprintf( stderr, _(" -u include User Friendly entry names in the output\n"));
94 fprintf( stderr, _(" -z limit size limit (in entries) for search\n"));
99 static void print_entry LDAP_P((
104 static void print_reference(
106 LDAPMessage *reference );
108 static void print_extended(
110 LDAPMessage *extended );
112 static void print_partial(
114 LDAPMessage *partial );
116 static int print_result(
121 static void print_ctrls(
122 LDAPControl **ctrls );
124 static int write_ldif LDAP_P((
130 static int dosearch LDAP_P((
138 LDAPControl **sctrls,
139 LDAPControl **cctrls,
140 struct timeval *timeout,
143 static char *tmpdir = NULL;
144 static char *urlpre = NULL;
145 static char *base = NULL;
146 static char *sortattr = NULL;
147 static int includeufn, vals2tmp = 0, ldif = 0;
149 static int subentries = 0, valuesReturnFilter = 0;
150 static char *vrFilter = NULL;
152 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
153 static int domainScope = 0;
157 static int ldapsync = 0;
158 static struct berval sync_cookie = { 0, NULL };
159 static int sync_slimit = -1;
162 #ifdef LDAP_CONTROL_PAGEDRESULTS
163 static int pagedResults = 0;
164 static ber_int_t pageSize = 0;
165 static ber_int_t entriesLeft = 0;
166 static ber_int_t morePagedResults = 1;
167 static struct berval cookie = { 0, NULL };
168 static int npagedresponses;
169 static int npagedentries;
170 static int npagedreferences;
171 static int npagedextended;
172 static int npagedpartial;
174 static int parse_page_control(
177 struct berval *cookie );
185 if (*LDAP_DIRSEP != '/') {
186 for (p = url; *p; p++) {
187 if (*p == *LDAP_DIRSEP)
194 const char options[] = "a:Ab:E:F:l:Ls:S:tT:uz:"
195 "Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
198 handle_private_option( int i )
201 char *control, *cvalue;
203 case 'a': /* set alias deref option */
204 if ( strcasecmp( optarg, "never" ) == 0 ) {
205 deref = LDAP_DEREF_NEVER;
206 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
207 deref = LDAP_DEREF_SEARCHING;
208 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
209 deref = LDAP_DEREF_FINDING;
210 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
211 deref = LDAP_DEREF_ALWAYS;
213 fprintf( stderr, _("alias deref should be never, search, find, or always\n") );
217 case 'A': /* retrieve attribute names only -- no values */
220 case 'b': /* search base */
221 base = strdup( optarg );
223 case 'E': /* search controls */
224 if( protocol == LDAP_VERSION2 ) {
225 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
227 exit( EXIT_FAILURE );
230 /* should be extended to support comma separated list of
231 * [!]key[=value] parameters, e.g. -E !foo,bar=567
236 if( optarg[0] == '!' ) {
241 control = strdup( optarg );
242 if ( (cvalue = strchr( control, '=' )) != NULL ) {
246 if ( strcasecmp( control, "mv" ) == 0 ) {
247 /* ValuesReturnFilter control */
248 if( valuesReturnFilter ) {
249 fprintf( stderr, _("ValuesReturnFilter previously specified\n"));
250 exit( EXIT_FAILURE );
252 valuesReturnFilter= 1 + crit;
254 if ( cvalue == NULL ) {
256 _("missing filter in ValuesReturnFilter control\n"));
257 exit( EXIT_FAILURE );
261 protocol = LDAP_VERSION3;
263 #ifdef LDAP_CONTROL_PAGEDRESULTS
264 } else if ( strcasecmp( control, "pr" ) == 0 ) {
266 /* PagedResults control */
267 if ( pagedResults != 0 ) {
268 fprintf( stderr, _("PagedResultsControl previously specified\n") );
269 exit( EXIT_FAILURE );
272 num = sscanf( cvalue, "%d", &tmp );
274 fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), cvalue);
275 exit( EXIT_FAILURE );
278 pageSize = (ber_int_t) tmp;
279 pagedResults = 1 + crit;
282 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
283 } else if ( strcasecmp( control, "domainScope" ) == 0 ) {
286 _("domainScope control previously specified\n"));
287 exit( EXIT_FAILURE );
289 if( cvalue != NULL ) {
291 _("domainScope: no control value expected\n") );
295 domainScope = 1 + crit;
297 #ifdef LDAP_CONTROL_SUBENTRIES
298 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
300 fprintf( stderr, _("subentries control previously specified\n"));
301 exit( EXIT_FAILURE );
303 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
305 } else if ( strcasecmp( cvalue, "false") == 0 ) {
309 _("subentries control value \"%s\" invalid\n"),
311 exit( EXIT_FAILURE );
313 if( crit ) subentries *= -1;
317 } else if ( strcasecmp( control, "sync" ) == 0 ) {
321 fprintf( stderr, _("ldap sync control previously specified\n") );
322 exit( EXIT_FAILURE );
324 if ( cvalue == NULL ) {
326 _("missing specification of ldap sync control\n"));
327 exit( EXIT_FAILURE );
329 if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
330 ldapsync = LDAP_SYNC_REFRESH_ONLY;
331 cookiep = strchr( cvalue, '/' );
332 if ( cookiep != NULL ) {
334 if ( *cookiep != '\0' ) {
335 ber_str2bv( cookiep, 0, 0, &sync_cookie );
338 } else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
339 ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
340 cookiep = strchr( cvalue, '/' );
341 if ( cookiep != NULL ) {
345 slimitp = strchr( cvalue, '/' );
346 if ( slimitp != NULL ) {
349 if ( cookiep != NULL && *cookiep != '\0' )
350 ber_str2bv( cookiep, 0, 0, &sync_cookie );
351 if ( slimitp != NULL && *slimitp != '\0' )
352 sync_slimit = atoi( slimitp );
355 _("ldap sync control value \"%s\" invalid\n"),
357 exit( EXIT_FAILURE );
359 if ( crit ) ldapsync *= -1;
363 fprintf( stderr, _("Invalid control name: %s\n"), control );
367 case 'F': /* uri prefix */
368 if( urlpre ) free( urlpre );
369 urlpre = strdup( optarg );
371 case 'l': /* time limit */
372 timelimit = atoi( optarg );
373 if( timelimit < 0 ) {
374 fprintf( stderr, _("%s: invalid timelimit (%d) specified\n"),
376 exit( EXIT_FAILURE );
379 case 'L': /* print entries in LDIF format */
382 case 's': /* search scope */
383 if ( strcasecmp( optarg, "base" ) == 0 ) {
384 scope = LDAP_SCOPE_BASE;
385 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
386 scope = LDAP_SCOPE_ONELEVEL;
387 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
388 scope = LDAP_SCOPE_SUBTREE;
390 fprintf( stderr, _("scope should be base, one, or sub\n") );
394 case 'S': /* sort attribute */
395 sortattr = strdup( optarg );
397 case 't': /* write attribute values to TMPDIR files */
400 case 'T': /* tmpdir */
401 if( tmpdir ) free( tmpdir );
402 tmpdir = strdup( optarg );
404 case 'u': /* include UFN */
407 case 'z': /* size limit */
408 sizelimit = atoi( optarg );
418 private_conn_setup( LDAP *ld )
421 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
423 fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
424 exit( EXIT_FAILURE );
426 if (timelimit != -1 &&
427 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
429 fprintf( stderr, _("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
430 exit( EXIT_FAILURE );
432 if (sizelimit != -1 &&
433 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
435 fprintf( stderr, _("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
436 exit( EXIT_FAILURE );
442 main( int argc, char **argv )
444 char *filtpattern, **attrs = NULL, line[BUFSIZ];
448 BerElement *seber = NULL, *vrber = NULL, *prber = NULL;
451 BerElement *syncber = NULL;
452 struct berval *syncbvalp = NULL;
457 #ifdef LDAP_CONTROL_PAGEDRESULTS
458 npagedresponses = npagedentries = npagedreferences =
459 npagedextended = npagedpartial = 0;
462 prog = lutil_progname( "ldapsearch", argc, argv );
464 lutil_log_initialize(argc, argv);
466 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
467 (def_tmpdir = getenv("TMP")) == NULL &&
468 (def_tmpdir = getenv("TEMP")) == NULL )
470 def_tmpdir = LDAP_TMPDIR;
474 def_tmpdir = LDAP_TMPDIR;
476 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
478 if( def_urlpre == NULL ) {
483 sprintf( def_urlpre, "file:///%s/",
484 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
486 urlize( def_urlpre );
488 tool_args( argc, argv );
490 if (( argc - optind < 1 ) ||
491 ( *argv[optind] != '(' /*')'*/ &&
492 ( strchr( argv[optind], '=' ) == NULL ) ) )
494 filtpattern = "(objectclass=*)";
496 filtpattern = strdup( argv[optind++] );
499 if ( argv[optind] != NULL ) {
500 attrs = &argv[optind];
503 if ( infile != NULL ) {
504 if ( infile[0] == '-' && infile[1] == '\0' ) {
506 } else if (( fp = fopen( infile, "r" )) == NULL ) {
512 if ( tmpdir == NULL ) {
515 if ( urlpre == NULL )
519 if( urlpre == NULL ) {
520 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
522 if( urlpre == NULL ) {
527 sprintf( urlpre, "file:///%s/",
528 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
536 ld = tool_conn_setup( 0, &private_conn_setup );
538 if ( pw_file || want_bindpw ) {
540 rc = lutil_get_filed_password( pw_file, &passwd );
541 if( rc ) return EXIT_FAILURE;
543 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
544 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
551 if ( assertion || authzid || manageDSAit || noop
552 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
555 #ifdef LDAP_CONTROL_PAGEDRESULTS
561 || subentries || valuesReturnFilter )
567 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
569 c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
570 c[i].ldctl_value.bv_val = NULL;
571 c[i].ldctl_value.bv_len = 0;
572 c[i].ldctl_iscritical = domainScope > 1;
577 #ifdef LDAP_CONTROL_SUBENTRIES
579 if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
583 err = ber_printf( seber, "{b}", abs(subentries) == 1 ? 0 : 1 );
585 ber_free( seber, 1 );
586 fprintf( stderr, _("Subentries control encoding error!\n") );
590 if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
594 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
595 c[i].ldctl_iscritical = subentries < 1;
602 if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
606 if ( sync_cookie.bv_len == 0 ) {
607 err = ber_printf( syncber, "{e}", abs(ldapsync) );
609 err = ber_printf( syncber, "{eO}", abs(ldapsync),
613 if ( err == LBER_ERROR ) {
614 ber_free( syncber, 1 );
615 fprintf( stderr, _("ldap sync control encoding error!\n") );
619 if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
623 c[i].ldctl_oid = LDAP_CONTROL_SYNC;
624 c[i].ldctl_value = (*syncbvalp);
625 c[i].ldctl_iscritical = ldapsync < 0;
630 if ( valuesReturnFilter ) {
631 if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
635 if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
636 ber_free( vrber, 1 );
637 fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
641 if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
645 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
646 c[i].ldctl_iscritical = valuesReturnFilter > 1;
650 #ifdef LDAP_CONTROL_PAGEDRESULTS
651 if ( pagedResults ) {
652 if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
656 ber_printf( prber, "{iO}", pageSize, &cookie );
657 if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == -1 ) {
661 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
662 c[i].ldctl_iscritical = pagedResults > 1;
667 tool_server_controls( ld, c, i );
669 ber_free( seber, 1 );
670 ber_free( vrber, 1 );
671 ber_free( prber, 1 );
675 fprintf( stderr, _("filter%s: %s\nrequesting: "),
676 infile != NULL ? _(" pattern") : "",
679 if ( attrs == NULL ) {
680 fprintf( stderr, _("ALL") );
682 for ( i = 0; attrs[ i ] != NULL; ++i ) {
683 fprintf( stderr, "%s ", attrs[ i ] );
686 fprintf( stderr, "\n" );
690 printf( _("# extended LDIF\n") );
691 } else if ( ldif < 3 ) {
692 printf( _("version: %d\n\n"), 1 );
697 printf(_("# LDAPv%d\n"), protocol);
698 printf(_("# base <%s> with scope %s\n"),
699 base ? base : "", (scope == LDAP_SCOPE_BASE) ? "base"
700 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"));
701 printf(_("# filter%s: %s\n"), infile != NULL ? _(" pattern") : "",
703 printf(_("# requesting: "));
705 if ( attrs == NULL ) {
708 for ( i = 0; attrs[ i ] != NULL; ++i ) {
709 printf( "%s ", attrs[ i ] );
714 printf(_("\n# with manageDSAit %scontrol"),
715 manageDSAit > 1 ? _("critical ") : "" );
718 printf(_("\n# with noop %scontrol"),
719 noop > 1 ? _("critical ") : "" );
722 printf(_("\n# with subentries %scontrol: %s"),
723 subentries < 0 ? _("critical ") : "",
724 abs(subentries) == 1 ? "false" : "true" );
726 if ( valuesReturnFilter ) {
727 printf(_("\n# with valuesReturnFilter %scontrol: %s"),
728 valuesReturnFilter > 1 ? _("critical ") : "", vrFilter );
730 #ifdef LDAP_CONTROL_PAGEDRESULTS
732 printf(_("\n# with pagedResults %scontrol: size=%d"),
733 (pagedResults > 1) ? _("critical ") : "",
738 printf( _("\n#\n\n") );
741 if ( infile == NULL ) {
742 rc = dosearch( ld, base, scope, NULL, filtpattern,
743 attrs, attrsonly, NULL, NULL, NULL, -1 );
748 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
749 line[ strlen( line ) - 1 ] = '\0';
755 rc = dosearch( ld, base, scope, filtpattern, line,
756 attrs, attrsonly, NULL, NULL, NULL, -1 );
763 #ifdef LDAP_CONTROL_PAGEDRESULTS
764 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
766 int i, moreEntries, tmpSize;
768 /* Loop to get the next pages when
769 * enter is pressed on the terminal.
771 if ( entriesLeft > 0 ) {
772 printf( _("Estimate entries: %d\n"), entriesLeft );
774 printf( _("Press [size] Enter for the next {%d|size} entries.\n"),
777 moreEntries = getchar();
778 while ( moreEntries != EOF && moreEntries != '\n' ) {
779 if ( i < (int)sizeof(buf) - 1 ) {
780 buf[i] = moreEntries;
783 moreEntries = getchar();
787 if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
788 int num = sscanf( buf, "%d", &tmpSize );
790 fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), buf);
794 pageSize = (ber_int_t)tmpSize;
802 #ifdef HAVE_CYRUS_SASL
806 ldap_pvt_tls_destroy();
820 LDAPControl **sctrls,
821 LDAPControl **cctrls,
822 struct timeval *timeout,
832 LDAPMessage *res, *msg;
836 struct berval *retdata = NULL;
837 int nresponses_psearch = -1;
838 int cancel_msgid = -1;
841 if( filtpatt != NULL ) {
842 filter = malloc( strlen( filtpatt ) + strlen( value ) );
843 if( filter == NULL ) {
848 sprintf( filter, filtpatt, value );
851 fprintf( stderr, _("filter: %s\n"), filter );
855 printf( _("#\n# filter: %s\n#\n"), filter );
866 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
867 sctrls, cctrls, timeout, sizelimit, &msgid );
869 if ( filtpatt != NULL ) {
873 if( rc != LDAP_SUCCESS ) {
874 fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
875 prog, ldap_err2string( rc ), rc );
879 nresponses = nentries = nreferences = nextended = npartial = 0;
883 while ((rc = ldap_result( ld, LDAP_RES_ANY,
884 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
888 (void) ldap_sort_entries( ld, &res,
889 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
892 for ( msg = ldap_first_message( ld, res );
894 msg = ldap_next_message( ld, msg ) )
896 if ( nresponses++ ) putchar('\n');
898 if ( nresponses_psearch >= 0 )
899 nresponses_psearch++;
902 switch( ldap_msgtype( msg ) ) {
903 case LDAP_RES_SEARCH_ENTRY:
905 print_entry( ld, msg, attrsonly );
908 case LDAP_RES_SEARCH_REFERENCE:
910 print_reference( ld, msg );
913 case LDAP_RES_EXTENDED:
915 print_extended( ld, msg );
917 if( ldap_msgid( msg ) == 0 ) {
918 /* unsolicited extended operation */
923 if ( cancel_msgid != -1 &&
924 cancel_msgid == ldap_msgid( msg ) ) {
925 printf(_("Cancelled \n"));
926 printf(_("cancel_msgid = %d\n"), cancel_msgid);
932 case LDAP_RES_SEARCH_RESULT:
933 rc = print_result( ld, msg, 1 );
934 #ifdef LDAP_CONTROL_PAGEDRESULTS
935 if ( pageSize != 0 ) {
936 rc = parse_page_control( ld, msg, &cookie );
941 if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
948 case LDAP_RES_INTERMEDIATE:
951 print_partial( ld, msg );
953 ldap_parse_intermediate( ld, msg,
954 &retoid, &retdata, NULL, 0 );
956 nresponses_psearch = 0;
958 if ( strcmp( retoid, LDAP_SYNC_INFO ) == 0 ) {
959 printf(_("SyncInfo Received\n"));
960 ldap_memfree( retoid );
961 ber_bvfree( retdata );
965 print_partial( ld, msg );
966 ldap_memfree( retoid );
967 ber_bvfree( retdata );
973 if ( ldapsync && sync_slimit != -1 &&
974 nresponses_psearch >= sync_slimit ) {
975 BerElement *msgidber = NULL;
976 struct berval *msgidvalp = NULL;
977 msgidber = ber_alloc_t(LBER_USE_DER);
978 ber_printf(msgidber, "{i}", msgid);
979 ber_flatten(msgidber, &msgidvalp);
980 ldap_extended_operation(ld, LDAP_EXOP_X_CANCEL,
981 msgidvalp, NULL, NULL, &cancel_msgid);
982 nresponses_psearch = -1;
992 ldap_perror( ld, "ldap_result" );
998 #ifdef LDAP_CONTROL_PAGEDRESULTS
999 if ( pageSize != 0 ) {
1000 npagedresponses = npagedresponses + nresponses;
1001 npagedentries = npagedentries + nentries;
1002 npagedreferences = npagedreferences + nreferences;
1003 npagedextended = npagedextended + nextended;
1004 npagedpartial = npagedpartial + npartial;
1005 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
1006 printf( _("\n# numResponses: %d\n"), npagedresponses );
1007 if( nentries ) printf( _("# numEntries: %d\n"), npagedentries );
1008 if( nextended ) printf( _("# numExtended: %d\n"), npagedextended );
1009 if( npartial ) printf( _("# numPartial: %d\n"), npagedpartial );
1010 if( nreferences ) printf( _("# numReferences: %d\n"), npagedreferences );
1015 printf( _("\n# numResponses: %d\n"), nresponses );
1016 if( nentries ) printf( _("# numEntries: %d\n"), nentries );
1017 if( nextended ) printf( _("# numExtended: %d\n"), nextended );
1018 if( npartial ) printf( _("# numPartial: %d\n"), npartial );
1019 if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
1026 /* This is the original version, the old way of doing things. */
1034 char tmpfname[ 256 ];
1037 BerElement *ber = NULL;
1038 struct berval **bvals;
1039 LDAPControl **ctrls = NULL;
1042 dn = ldap_get_dn( ld, entry );
1046 ufn = ldap_dn2ufn( dn );
1047 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1049 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1051 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1053 if( rc != LDAP_SUCCESS ) {
1054 fprintf(stderr, _("print_entry: %d\n"), rc );
1055 ldap_perror( ld, "ldap_get_entry_controls" );
1056 exit( EXIT_FAILURE );
1060 print_ctrls( ctrls );
1061 ldap_controls_free( ctrls );
1066 ufn = ldap_dn2ufn( dn );
1068 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1071 if( ufn != NULL ) ldap_memfree( ufn );
1074 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1075 a = ldap_next_attribute( ld, entry, ber ) )
1078 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1080 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1081 for ( i = 0; bvals[i] != NULL; i++ ) {
1082 if ( vals2tmp > 1 || ( vals2tmp
1083 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1086 /* write value to file */
1087 snprintf( tmpfname, sizeof tmpfname,
1088 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1092 tmpfd = mkstemp( tmpfname );
1099 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1104 if ( fwrite( bvals[ i ]->bv_val,
1105 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1114 snprintf( url, sizeof url, "%s%s", urlpre,
1115 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1118 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1121 write_ldif( LDIF_PUT_VALUE, a,
1122 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1125 ber_bvecfree( bvals );
1135 /* This is the proposed new way of doing things.
1136 * It is more efficient, but the API is non-standard.
1145 char tmpfname[ 256 ];
1148 BerElement *ber = NULL;
1149 struct berval bv, *bvals, **bvp = &bvals;
1150 LDAPControl **ctrls = NULL;
1153 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1156 ufn = ldap_dn2ufn( bv.bv_val );
1157 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1159 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1161 rc = ldap_int_get_controls( ber, &ctrls );
1163 if( rc != LDAP_SUCCESS ) {
1164 fprintf(stderr, _("print_entry: %d\n"), rc );
1165 ldap_perror( ld, "ldap_get_entry_controls" );
1166 exit( EXIT_FAILURE );
1170 print_ctrls( ctrls );
1171 ldap_controls_free( ctrls );
1176 ufn = ldap_dn2ufn( bv.bv_val );
1178 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1181 if( ufn != NULL ) ldap_memfree( ufn );
1183 if ( attrsonly ) bvp = NULL;
1185 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1187 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1189 if (bv.bv_val == NULL) break;
1191 if ( attrsonly || !bvals ) {
1192 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1195 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1196 if ( vals2tmp > 1 || ( vals2tmp
1197 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1200 /* write value to file */
1201 snprintf( tmpfname, sizeof tmpfname,
1202 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1203 tmpdir, bv.bv_val );
1206 tmpfd = mkstemp( tmpfname );
1213 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1218 if ( fwrite( bvals[ i ].bv_val,
1219 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1228 snprintf( url, sizeof url, "%s%s", urlpre,
1229 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1232 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1235 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1236 bvals[ i ].bv_val, bvals[ i ].bv_len );
1239 ber_memfree( bvals );
1249 static void print_reference(
1251 LDAPMessage *reference )
1255 LDAPControl **ctrls;
1258 printf(_("# search reference\n"));
1261 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1263 if( rc != LDAP_SUCCESS ) {
1264 ldap_perror(ld, "ldap_parse_reference");
1265 exit( EXIT_FAILURE );
1270 for( i=0; refs[i] != NULL; i++ ) {
1271 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1272 "ref", refs[i], strlen(refs[i]) );
1274 ber_memvfree( (void **) refs );
1278 print_ctrls( ctrls );
1279 ldap_controls_free( ctrls );
1283 static void print_extended(
1285 LDAPMessage *extended )
1288 char *retoid = NULL;
1289 struct berval *retdata = NULL;
1292 printf(_("# extended result response\n"));
1295 rc = ldap_parse_extended_result( ld, extended,
1296 &retoid, &retdata, 0 );
1298 if( rc != LDAP_SUCCESS ) {
1299 ldap_perror(ld, "ldap_parse_extended_result");
1300 exit( EXIT_FAILURE );
1304 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1305 "extended", retoid, retoid ? strlen(retoid) : 0 );
1307 ber_memfree( retoid );
1311 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1312 "data", retdata->bv_val, retdata->bv_len );
1314 ber_bvfree( retdata );
1317 print_result( ld, extended, 0 );
1320 static void print_partial(
1322 LDAPMessage *partial )
1325 char *retoid = NULL;
1326 struct berval *retdata = NULL;
1327 LDAPControl **ctrls = NULL;
1330 printf(_("# extended partial response\n"));
1333 rc = ldap_parse_intermediate( ld, partial,
1334 &retoid, &retdata, &ctrls, 0 );
1336 if( rc != LDAP_SUCCESS ) {
1337 ldap_perror(ld, "ldap_parse_intermediate");
1338 exit( EXIT_FAILURE );
1342 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1343 "partial", retoid, retoid ? strlen(retoid) : 0 );
1346 ber_memfree( retoid );
1350 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1351 "data", retdata->bv_val, retdata->bv_len );
1354 ber_bvfree( retdata );
1358 print_ctrls( ctrls );
1359 ldap_controls_free( ctrls );
1363 static int print_result(
1365 LDAPMessage *result, int search )
1369 char *matcheddn = NULL;
1372 LDAPControl **ctrls = NULL;
1376 printf(_("# search result\n"));
1379 printf("%s: %d\n", _("search"), ldap_msgid(result) );
1383 rc = ldap_parse_result( ld, result,
1384 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1386 if( rc != LDAP_SUCCESS ) {
1387 ldap_perror(ld, "ldap_parse_result");
1388 exit( EXIT_FAILURE );
1393 printf( _("result: %d %s\n"), err, ldap_err2string(err) );
1395 } else if ( err != LDAP_SUCCESS ) {
1396 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1402 write_ldif( LDIF_PUT_VALUE,
1403 "matchedDN", matcheddn, strlen(matcheddn) );
1405 fprintf( stderr, _("Matched DN: %s\n"), matcheddn );
1409 ber_memfree( matcheddn );
1415 write_ldif( LDIF_PUT_TEXT, "text",
1416 text, strlen(text) );
1418 fprintf( stderr, _("Additional information: %s\n"), text );
1422 ber_memfree( text );
1427 for( i=0; refs[i] != NULL; i++ ) {
1429 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1431 fprintf( stderr, _("Referral: %s\n"), refs[i] );
1435 ber_memvfree( (void **) refs );
1439 print_ctrls( ctrls );
1440 ldap_controls_free( ctrls );
1446 static void print_ctrls(
1447 LDAPControl **ctrls )
1450 for(i=0; ctrls[i] != NULL; i++ ) {
1451 /* control: OID criticality base64value */
1452 struct berval *b64 = NULL;
1457 len += strlen( ctrls[i]->ldctl_oid );
1459 /* add enough for space after OID and the critical value itself */
1460 len += ctrls[i]->ldctl_iscritical
1461 ? sizeof("true") : sizeof("false");
1463 /* convert to base64 */
1464 if( ctrls[i]->ldctl_value.bv_len ) {
1465 b64 = ber_memalloc( sizeof(struct berval) );
1467 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1468 ctrls[i]->ldctl_value.bv_len ) + 1;
1469 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1471 b64->bv_len = lutil_b64_ntop(
1472 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1473 ctrls[i]->ldctl_value.bv_len,
1474 b64->bv_val, b64->bv_len );
1478 len += 1 + b64->bv_len;
1481 str = malloc( len + 1 );
1483 strcpy( str, ": " );
1487 strcat( str, ctrls[i]->ldctl_oid );
1488 strcat( str, ctrls[i]->ldctl_iscritical
1489 ? " true" : " false" );
1493 strcat(str, b64->bv_val );
1497 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1498 "control", str, len );
1507 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1511 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1515 fputs( ldif, stdout );
1516 ber_memfree( ldif );
1522 #ifdef LDAP_CONTROL_PAGEDRESULTS
1526 LDAPMessage *result,
1527 struct berval *cookie )
1531 LDAPControl **ctrl = NULL;
1532 LDAPControl *ctrlp = NULL;
1535 struct berval servercookie = { 0, NULL };
1537 rc = ldap_parse_result( ld, result,
1538 &err, NULL, NULL, NULL, &ctrl, 0 );
1540 if( rc != LDAP_SUCCESS ) {
1541 ldap_perror(ld, "ldap_parse_result");
1542 exit( EXIT_FAILURE );
1545 if ( err != LDAP_SUCCESS ) {
1546 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1550 /* Parse the control value
1551 * searchResult ::= SEQUENCE {
1552 * size INTEGER (0..maxInt),
1553 * -- result set size estimate from server - unused
1554 * cookie OCTET STRING
1558 ber = ber_init( &ctrlp->ldctl_value );
1559 if ( ber == NULL ) {
1560 fprintf( stderr, _("Internal error.\n") );
1561 return EXIT_FAILURE;
1564 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1565 ber_dupbv( cookie, &servercookie );
1566 (void) ber_free( ber, 1 );
1568 if( tag == LBER_ERROR ) {
1570 _("Paged results response control could not be decoded.\n") );
1571 return EXIT_FAILURE;
1574 if( entriesLeft < 0 ) {
1576 _("Invalid entries estimate in paged results response.\n") );
1577 return EXIT_FAILURE;
1580 ldap_controls_free( ctrl );
1583 morePagedResults = 0;