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"));
78 fprintf( stderr, _(" [!]sync=ro[/<cookie>] (LDAP Sync refreshOnly)\n"));
79 fprintf( stderr, _(" rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
80 fprintf( stderr, _(" -F prefix URL prefix for files (default: %s)\n"), def_urlpre);
81 fprintf( stderr, _(" -l limit time limit (in seconds) for search\n"));
82 fprintf( stderr, _(" -L print responses in LDIFv1 format\n"));
83 fprintf( stderr, _(" -LL print responses in LDIF format without comments\n"));
84 fprintf( stderr, _(" -LLL print responses in LDIF format without comments\n"));
85 fprintf( stderr, _(" and version\n"));
86 fprintf( stderr, _(" -s scope one of base, one, or sub (search scope)\n"));
87 fprintf( stderr, _(" -S attr sort the results by attribute `attr'\n"));
88 fprintf( stderr, _(" -t write binary values to files in temporary directory\n"));
89 fprintf( stderr, _(" -tt write all values to files in temporary directory\n"));
90 fprintf( stderr, _(" -T path write files to directory specified by path (default: %s)\n"), def_tmpdir);
91 fprintf( stderr, _(" -u include User Friendly entry names in the output\n"));
92 fprintf( stderr, _(" -z limit size limit (in entries) for search\n"));
97 static void print_entry LDAP_P((
102 static void print_reference(
104 LDAPMessage *reference );
106 static void print_extended(
108 LDAPMessage *extended );
110 static void print_partial(
112 LDAPMessage *partial );
114 static int print_result(
119 static void print_ctrls(
120 LDAPControl **ctrls );
122 static int write_ldif LDAP_P((
128 static int dosearch LDAP_P((
136 LDAPControl **sctrls,
137 LDAPControl **cctrls,
138 struct timeval *timeout,
141 static char *tmpdir = NULL;
142 static char *urlpre = NULL;
143 static char *base = NULL;
144 static char *sortattr = NULL;
145 static int includeufn, vals2tmp = 0, ldif = 0;
147 static int subentries = 0, valuesReturnFilter = 0;
148 static char *vrFilter = NULL;
150 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
151 static int domainScope = 0;
154 static int ldapsync = 0;
155 static struct berval sync_cookie = { 0, NULL };
156 static int sync_slimit = -1;
158 #ifdef LDAP_CONTROL_PAGEDRESULTS
159 static int pagedResults = 0;
160 static ber_int_t pageSize = 0;
161 static ber_int_t entriesLeft = 0;
162 static ber_int_t morePagedResults = 1;
163 static struct berval cookie = { 0, NULL };
164 static int npagedresponses;
165 static int npagedentries;
166 static int npagedreferences;
167 static int npagedextended;
168 static int npagedpartial;
170 static int parse_page_control(
173 struct berval *cookie );
181 if (*LDAP_DIRSEP != '/') {
182 for (p = url; *p; p++) {
183 if (*p == *LDAP_DIRSEP)
190 const char options[] = "a:Ab:E:F:l:Ls:S:tT:uz:"
191 "Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
194 handle_private_option( int i )
197 char *control, *cvalue;
199 case 'a': /* set alias deref option */
200 if ( strcasecmp( optarg, "never" ) == 0 ) {
201 deref = LDAP_DEREF_NEVER;
202 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
203 deref = LDAP_DEREF_SEARCHING;
204 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
205 deref = LDAP_DEREF_FINDING;
206 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
207 deref = LDAP_DEREF_ALWAYS;
209 fprintf( stderr, _("alias deref should be never, search, find, or always\n") );
213 case 'A': /* retrieve attribute names only -- no values */
216 case 'b': /* search base */
217 base = strdup( optarg );
219 case 'E': /* search controls */
220 if( protocol == LDAP_VERSION2 ) {
221 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
223 exit( EXIT_FAILURE );
226 /* should be extended to support comma separated list of
227 * [!]key[=value] parameters, e.g. -E !foo,bar=567
232 if( optarg[0] == '!' ) {
237 control = strdup( optarg );
238 if ( (cvalue = strchr( control, '=' )) != NULL ) {
242 if ( strcasecmp( control, "mv" ) == 0 ) {
243 /* ValuesReturnFilter control */
244 if( valuesReturnFilter ) {
245 fprintf( stderr, _("ValuesReturnFilter previously specified\n"));
246 exit( EXIT_FAILURE );
248 valuesReturnFilter= 1 + crit;
250 if ( cvalue == NULL ) {
252 _("missing filter in ValuesReturnFilter control\n"));
253 exit( EXIT_FAILURE );
257 protocol = LDAP_VERSION3;
259 #ifdef LDAP_CONTROL_PAGEDRESULTS
260 } else if ( strcasecmp( control, "pr" ) == 0 ) {
262 /* PagedResults control */
263 if ( pagedResults != 0 ) {
264 fprintf( stderr, _("PagedResultsControl previously specified\n") );
265 exit( EXIT_FAILURE );
268 num = sscanf( cvalue, "%d", &tmp );
270 fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), cvalue);
271 exit( EXIT_FAILURE );
274 pageSize = (ber_int_t) tmp;
275 pagedResults = 1 + crit;
278 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
279 } else if ( strcasecmp( control, "domainScope" ) == 0 ) {
282 _("domainScope control previously specified\n"));
283 exit( EXIT_FAILURE );
285 if( cvalue != NULL ) {
287 _("domainScope: no control value expected\n") );
291 domainScope = 1 + crit;
293 #ifdef LDAP_CONTROL_SUBENTRIES
294 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
296 fprintf( stderr, _("subentries control previously specified\n"));
297 exit( EXIT_FAILURE );
299 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
301 } else if ( strcasecmp( cvalue, "false") == 0 ) {
305 _("subentries control value \"%s\" invalid\n"),
307 exit( EXIT_FAILURE );
309 if( crit ) subentries *= -1;
312 } else if ( strcasecmp( control, "sync" ) == 0 ) {
316 fprintf( stderr, _("ldap sync control previously specified\n") );
317 exit( EXIT_FAILURE );
319 if ( cvalue == NULL ) {
321 _("missing specification of ldap sync control\n"));
322 exit( EXIT_FAILURE );
324 if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
325 ldapsync = LDAP_SYNC_REFRESH_ONLY;
326 cookiep = strchr( cvalue, '/' );
327 if ( cookiep != NULL ) {
329 if ( *cookiep != '\0' ) {
330 ber_str2bv( cookiep, 0, 0, &sync_cookie );
333 } else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
334 ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
335 cookiep = strchr( cvalue, '/' );
336 if ( cookiep != NULL ) {
340 slimitp = strchr( cvalue, '/' );
341 if ( slimitp != NULL ) {
344 if ( cookiep != NULL && *cookiep != '\0' )
345 ber_str2bv( cookiep, 0, 0, &sync_cookie );
346 if ( slimitp != NULL && *slimitp != '\0' )
347 sync_slimit = atoi( slimitp );
350 _("ldap sync control value \"%s\" invalid\n"),
352 exit( EXIT_FAILURE );
354 if ( crit ) ldapsync *= -1;
357 fprintf( stderr, _("Invalid control name: %s\n"), control );
361 case 'F': /* uri prefix */
362 if( urlpre ) free( urlpre );
363 urlpre = strdup( optarg );
365 case 'l': /* time limit */
366 timelimit = atoi( optarg );
367 if( timelimit < 0 ) {
368 fprintf( stderr, _("%s: invalid timelimit (%d) specified\n"),
370 exit( EXIT_FAILURE );
373 case 'L': /* print entries in LDIF format */
376 case 's': /* search scope */
377 if ( strcasecmp( optarg, "base" ) == 0 ) {
378 scope = LDAP_SCOPE_BASE;
379 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
380 scope = LDAP_SCOPE_ONELEVEL;
381 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
382 scope = LDAP_SCOPE_SUBTREE;
384 fprintf( stderr, _("scope should be base, one, or sub\n") );
388 case 'S': /* sort attribute */
389 sortattr = strdup( optarg );
391 case 't': /* write attribute values to TMPDIR files */
394 case 'T': /* tmpdir */
395 if( tmpdir ) free( tmpdir );
396 tmpdir = strdup( optarg );
398 case 'u': /* include UFN */
401 case 'z': /* size limit */
402 sizelimit = atoi( optarg );
412 private_conn_setup( LDAP *ld )
415 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
417 fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
418 exit( EXIT_FAILURE );
420 if (timelimit != -1 &&
421 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
423 fprintf( stderr, _("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
424 exit( EXIT_FAILURE );
426 if (sizelimit != -1 &&
427 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
429 fprintf( stderr, _("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
430 exit( EXIT_FAILURE );
436 main( int argc, char **argv )
438 char *filtpattern, **attrs = NULL, line[BUFSIZ];
442 BerElement *seber = NULL, *vrber = NULL, *prber = NULL;
444 BerElement *syncber = NULL;
445 struct berval *syncbvalp = NULL;
449 #ifdef LDAP_CONTROL_PAGEDRESULTS
450 npagedresponses = npagedentries = npagedreferences =
451 npagedextended = npagedpartial = 0;
454 prog = lutil_progname( "ldapsearch", argc, argv );
456 lutil_log_initialize(argc, argv);
458 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
459 (def_tmpdir = getenv("TMP")) == NULL &&
460 (def_tmpdir = getenv("TEMP")) == NULL )
462 def_tmpdir = LDAP_TMPDIR;
466 def_tmpdir = LDAP_TMPDIR;
468 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
470 if( def_urlpre == NULL ) {
475 sprintf( def_urlpre, "file:///%s/",
476 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
478 urlize( def_urlpre );
480 tool_args( argc, argv );
482 if (( argc - optind < 1 ) ||
483 ( *argv[optind] != '(' /*')'*/ &&
484 ( strchr( argv[optind], '=' ) == NULL ) ) )
486 filtpattern = "(objectclass=*)";
488 filtpattern = strdup( argv[optind++] );
491 if ( argv[optind] != NULL ) {
492 attrs = &argv[optind];
495 if ( infile != NULL ) {
496 if ( infile[0] == '-' && infile[1] == '\0' ) {
498 } else if (( fp = fopen( infile, "r" )) == NULL ) {
504 if ( tmpdir == NULL ) {
507 if ( urlpre == NULL )
511 if( urlpre == NULL ) {
512 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
514 if( urlpre == NULL ) {
519 sprintf( urlpre, "file:///%s/",
520 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
528 ld = tool_conn_setup( 0, &private_conn_setup );
530 if ( pw_file || want_bindpw ) {
532 rc = lutil_get_filed_password( pw_file, &passwd );
533 if( rc ) return EXIT_FAILURE;
535 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
536 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
543 if ( assertion || authzid || manageDSAit || noop
544 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
547 #ifdef LDAP_CONTROL_PAGEDRESULTS
551 || subentries || valuesReturnFilter )
557 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
559 c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
560 c[i].ldctl_value.bv_val = NULL;
561 c[i].ldctl_value.bv_len = 0;
562 c[i].ldctl_iscritical = domainScope > 1;
567 #ifdef LDAP_CONTROL_SUBENTRIES
569 if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
573 err = ber_printf( seber, "{b}", abs(subentries) == 1 ? 0 : 1 );
575 ber_free( seber, 1 );
576 fprintf( stderr, _("Subentries control encoding error!\n") );
580 if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
584 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
585 c[i].ldctl_iscritical = subentries < 1;
591 if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
595 if ( sync_cookie.bv_len == 0 ) {
596 err = ber_printf( syncber, "{e}", abs(ldapsync) );
598 err = ber_printf( syncber, "{eO}", abs(ldapsync),
602 if ( err == LBER_ERROR ) {
603 ber_free( syncber, 1 );
604 fprintf( stderr, _("ldap sync control encoding error!\n") );
608 if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
612 c[i].ldctl_oid = LDAP_CONTROL_SYNC;
613 c[i].ldctl_value = (*syncbvalp);
614 c[i].ldctl_iscritical = ldapsync < 0;
618 if ( valuesReturnFilter ) {
619 if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
623 if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
624 ber_free( vrber, 1 );
625 fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
629 if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
633 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
634 c[i].ldctl_iscritical = valuesReturnFilter > 1;
638 #ifdef LDAP_CONTROL_PAGEDRESULTS
639 if ( pagedResults ) {
640 if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
644 ber_printf( prber, "{iO}", pageSize, &cookie );
645 if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == -1 ) {
649 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
650 c[i].ldctl_iscritical = pagedResults > 1;
655 tool_server_controls( ld, c, i );
657 ber_free( seber, 1 );
658 ber_free( vrber, 1 );
659 ber_free( prber, 1 );
663 fprintf( stderr, _("filter%s: %s\nrequesting: "),
664 infile != NULL ? _(" pattern") : "",
667 if ( attrs == NULL ) {
668 fprintf( stderr, _("ALL") );
670 for ( i = 0; attrs[ i ] != NULL; ++i ) {
671 fprintf( stderr, "%s ", attrs[ i ] );
674 fprintf( stderr, "\n" );
678 printf( _("# extended LDIF\n") );
679 } else if ( ldif < 3 ) {
680 printf( _("version: %d\n\n"), 1 );
685 printf(_("# LDAPv%d\n"), protocol);
686 printf(_("# base <%s> with scope %s\n"),
687 base ? base : "", (scope == LDAP_SCOPE_BASE) ? "base"
688 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"));
689 printf(_("# filter%s: %s\n"), infile != NULL ? _(" pattern") : "",
691 printf(_("# requesting: "));
693 if ( attrs == NULL ) {
696 for ( i = 0; attrs[ i ] != NULL; ++i ) {
697 printf( "%s ", attrs[ i ] );
702 printf(_("\n# with manageDSAit %scontrol"),
703 manageDSAit > 1 ? _("critical ") : "" );
706 printf(_("\n# with noop %scontrol"),
707 noop > 1 ? _("critical ") : "" );
710 printf(_("\n# with subentries %scontrol: %s"),
711 subentries < 0 ? _("critical ") : "",
712 abs(subentries) == 1 ? "false" : "true" );
714 if ( valuesReturnFilter ) {
715 printf(_("\n# with valuesReturnFilter %scontrol: %s"),
716 valuesReturnFilter > 1 ? _("critical ") : "", vrFilter );
718 #ifdef LDAP_CONTROL_PAGEDRESULTS
720 printf(_("\n# with pagedResults %scontrol: size=%d"),
721 (pagedResults > 1) ? _("critical ") : "",
726 printf( _("\n#\n\n") );
729 if ( infile == NULL ) {
730 rc = dosearch( ld, base, scope, NULL, filtpattern,
731 attrs, attrsonly, NULL, NULL, NULL, -1 );
736 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
737 line[ strlen( line ) - 1 ] = '\0';
743 rc = dosearch( ld, base, scope, filtpattern, line,
744 attrs, attrsonly, NULL, NULL, NULL, -1 );
751 #ifdef LDAP_CONTROL_PAGEDRESULTS
752 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
754 int i, moreEntries, tmpSize;
756 /* Loop to get the next pages when
757 * enter is pressed on the terminal.
759 if ( entriesLeft > 0 ) {
760 printf( _("Estimate entries: %d\n"), entriesLeft );
762 printf( _("Press [size] Enter for the next {%d|size} entries.\n"),
765 moreEntries = getchar();
766 while ( moreEntries != EOF && moreEntries != '\n' ) {
767 if ( i < (int)sizeof(buf) - 1 ) {
768 buf[i] = moreEntries;
771 moreEntries = getchar();
775 if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
776 int num = sscanf( buf, "%d", &tmpSize );
778 fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), buf);
782 pageSize = (ber_int_t)tmpSize;
790 #ifdef HAVE_CYRUS_SASL
794 ldap_pvt_tls_destroy();
808 LDAPControl **sctrls,
809 LDAPControl **cctrls,
810 struct timeval *timeout,
820 LDAPMessage *res, *msg;
823 struct berval *retdata = NULL;
824 int nresponses_psearch = -1;
825 int cancel_msgid = -1;
827 if( filtpatt != NULL ) {
828 filter = malloc( strlen( filtpatt ) + strlen( value ) );
829 if( filter == NULL ) {
834 sprintf( filter, filtpatt, value );
837 fprintf( stderr, _("filter: %s\n"), filter );
841 printf( _("#\n# filter: %s\n#\n"), filter );
852 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
853 sctrls, cctrls, timeout, sizelimit, &msgid );
855 if ( filtpatt != NULL ) {
859 if( rc != LDAP_SUCCESS ) {
860 fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
861 prog, ldap_err2string( rc ), rc );
865 nresponses = nentries = nreferences = nextended = npartial = 0;
869 while ((rc = ldap_result( ld, LDAP_RES_ANY,
870 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
874 (void) ldap_sort_entries( ld, &res,
875 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
878 for ( msg = ldap_first_message( ld, res );
880 msg = ldap_next_message( ld, msg ) )
882 if ( nresponses++ ) putchar('\n');
883 if ( nresponses_psearch >= 0 )
884 nresponses_psearch++;
886 switch( ldap_msgtype( msg ) ) {
887 case LDAP_RES_SEARCH_ENTRY:
889 print_entry( ld, msg, attrsonly );
892 case LDAP_RES_SEARCH_REFERENCE:
894 print_reference( ld, msg );
897 case LDAP_RES_EXTENDED:
899 print_extended( ld, msg );
901 if( ldap_msgid( msg ) == 0 ) {
902 /* unsolicited extended operation */
906 if ( cancel_msgid != -1 &&
907 cancel_msgid == ldap_msgid( msg ) ) {
908 printf(_("Cancelled \n"));
909 printf(_("cancel_msgid = %d\n"), cancel_msgid);
914 case LDAP_RES_SEARCH_RESULT:
915 rc = print_result( ld, msg, 1 );
916 #ifdef LDAP_CONTROL_PAGEDRESULTS
917 if ( pageSize != 0 ) {
918 rc = parse_page_control( ld, msg, &cookie );
922 if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
928 case LDAP_RES_INTERMEDIATE:
930 ldap_parse_intermediate( ld, msg,
931 &retoid, &retdata, NULL, 0 );
933 nresponses_psearch = 0;
935 if ( strcmp( retoid, LDAP_SYNC_INFO ) == 0 ) {
936 printf(_("SyncInfo Received\n"));
937 ldap_memfree( retoid );
938 ber_bvfree( retdata );
942 print_partial( ld, msg );
943 ldap_memfree( retoid );
944 ber_bvfree( retdata );
948 if ( ldapsync && sync_slimit != -1 &&
949 nresponses_psearch >= sync_slimit ) {
950 BerElement *msgidber = NULL;
951 struct berval *msgidvalp = NULL;
952 msgidber = ber_alloc_t(LBER_USE_DER);
953 ber_printf(msgidber, "{i}", msgid);
954 ber_flatten(msgidber, &msgidvalp);
955 ldap_extended_operation(ld, LDAP_EXOP_X_CANCEL,
956 msgidvalp, NULL, NULL, &cancel_msgid);
957 nresponses_psearch = -1;
965 ldap_perror( ld, "ldap_result" );
971 #ifdef LDAP_CONTROL_PAGEDRESULTS
972 if ( pageSize != 0 ) {
973 npagedresponses = npagedresponses + nresponses;
974 npagedentries = npagedentries + nentries;
975 npagedreferences = npagedreferences + nreferences;
976 npagedextended = npagedextended + nextended;
977 npagedpartial = npagedpartial + npartial;
978 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
979 printf( _("\n# numResponses: %d\n"), npagedresponses );
980 if( nentries ) printf( _("# numEntries: %d\n"), npagedentries );
981 if( nextended ) printf( _("# numExtended: %d\n"), npagedextended );
982 if( npartial ) printf( _("# numPartial: %d\n"), npagedpartial );
983 if( nreferences ) printf( _("# numReferences: %d\n"), npagedreferences );
988 printf( _("\n# numResponses: %d\n"), nresponses );
989 if( nentries ) printf( _("# numEntries: %d\n"), nentries );
990 if( nextended ) printf( _("# numExtended: %d\n"), nextended );
991 if( npartial ) printf( _("# numPartial: %d\n"), npartial );
992 if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
999 /* This is the original version, the old way of doing things. */
1007 char tmpfname[ 256 ];
1010 BerElement *ber = NULL;
1011 struct berval **bvals;
1012 LDAPControl **ctrls = NULL;
1015 dn = ldap_get_dn( ld, entry );
1019 ufn = ldap_dn2ufn( dn );
1020 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1022 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1024 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1026 if( rc != LDAP_SUCCESS ) {
1027 fprintf(stderr, _("print_entry: %d\n"), rc );
1028 ldap_perror( ld, "ldap_get_entry_controls" );
1029 exit( EXIT_FAILURE );
1033 print_ctrls( ctrls );
1034 ldap_controls_free( ctrls );
1039 ufn = ldap_dn2ufn( dn );
1041 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1044 if( ufn != NULL ) ldap_memfree( ufn );
1047 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1048 a = ldap_next_attribute( ld, entry, ber ) )
1051 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1053 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1054 for ( i = 0; bvals[i] != NULL; i++ ) {
1055 if ( vals2tmp > 1 || ( vals2tmp
1056 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1059 /* write value to file */
1060 snprintf( tmpfname, sizeof tmpfname,
1061 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1065 tmpfd = mkstemp( tmpfname );
1072 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1077 if ( fwrite( bvals[ i ]->bv_val,
1078 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1087 snprintf( url, sizeof url, "%s%s", urlpre,
1088 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1091 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1094 write_ldif( LDIF_PUT_VALUE, a,
1095 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1098 ber_bvecfree( bvals );
1108 /* This is the proposed new way of doing things.
1109 * It is more efficient, but the API is non-standard.
1118 char tmpfname[ 256 ];
1121 BerElement *ber = NULL;
1122 struct berval bv, *bvals, **bvp = &bvals;
1123 LDAPControl **ctrls = NULL;
1126 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1129 ufn = ldap_dn2ufn( bv.bv_val );
1130 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1132 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1134 rc = ldap_int_get_controls( ber, &ctrls );
1136 if( rc != LDAP_SUCCESS ) {
1137 fprintf(stderr, _("print_entry: %d\n"), rc );
1138 ldap_perror( ld, "ldap_get_entry_controls" );
1139 exit( EXIT_FAILURE );
1143 print_ctrls( ctrls );
1144 ldap_controls_free( ctrls );
1149 ufn = ldap_dn2ufn( bv.bv_val );
1151 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1154 if( ufn != NULL ) ldap_memfree( ufn );
1156 if ( attrsonly ) bvp = NULL;
1158 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1160 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1162 if (bv.bv_val == NULL) break;
1165 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1167 } else if ( bvals ) {
1168 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1169 if ( vals2tmp > 1 || ( vals2tmp
1170 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1173 /* write value to file */
1174 snprintf( tmpfname, sizeof tmpfname,
1175 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1176 tmpdir, bv.bv_val );
1179 tmpfd = mkstemp( tmpfname );
1186 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1191 if ( fwrite( bvals[ i ].bv_val,
1192 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1201 snprintf( url, sizeof url, "%s%s", urlpre,
1202 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1205 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1208 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1209 bvals[ i ].bv_val, bvals[ i ].bv_len );
1212 ber_memfree( bvals );
1222 static void print_reference(
1224 LDAPMessage *reference )
1228 LDAPControl **ctrls;
1231 printf(_("# search reference\n"));
1234 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1236 if( rc != LDAP_SUCCESS ) {
1237 ldap_perror(ld, "ldap_parse_reference");
1238 exit( EXIT_FAILURE );
1243 for( i=0; refs[i] != NULL; i++ ) {
1244 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1245 "ref", refs[i], strlen(refs[i]) );
1247 ber_memvfree( (void **) refs );
1251 print_ctrls( ctrls );
1252 ldap_controls_free( ctrls );
1256 static void print_extended(
1258 LDAPMessage *extended )
1261 char *retoid = NULL;
1262 struct berval *retdata = NULL;
1265 printf(_("# extended result response\n"));
1268 rc = ldap_parse_extended_result( ld, extended,
1269 &retoid, &retdata, 0 );
1271 if( rc != LDAP_SUCCESS ) {
1272 ldap_perror(ld, "ldap_parse_extended_result");
1273 exit( EXIT_FAILURE );
1277 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1278 "extended", retoid, retoid ? strlen(retoid) : 0 );
1280 ber_memfree( retoid );
1284 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1285 "data", retdata->bv_val, retdata->bv_len );
1287 ber_bvfree( retdata );
1290 print_result( ld, extended, 0 );
1293 static void print_partial(
1295 LDAPMessage *partial )
1298 char *retoid = NULL;
1299 struct berval *retdata = NULL;
1300 LDAPControl **ctrls = NULL;
1303 printf(_("# extended partial response\n"));
1306 rc = ldap_parse_intermediate( ld, partial,
1307 &retoid, &retdata, &ctrls, 0 );
1309 if( rc != LDAP_SUCCESS ) {
1310 ldap_perror(ld, "ldap_parse_intermediate");
1311 exit( EXIT_FAILURE );
1315 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1316 "partial", retoid, retoid ? strlen(retoid) : 0 );
1319 ber_memfree( retoid );
1323 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1324 "data", retdata->bv_val, retdata->bv_len );
1327 ber_bvfree( retdata );
1331 print_ctrls( ctrls );
1332 ldap_controls_free( ctrls );
1336 static int print_result(
1338 LDAPMessage *result, int search )
1342 char *matcheddn = NULL;
1345 LDAPControl **ctrls = NULL;
1349 printf(_("# search result\n"));
1352 printf("%s: %d\n", _("search"), ldap_msgid(result) );
1356 rc = ldap_parse_result( ld, result,
1357 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1359 if( rc != LDAP_SUCCESS ) {
1360 ldap_perror(ld, "ldap_parse_result");
1361 exit( EXIT_FAILURE );
1366 printf( _("result: %d %s\n"), err, ldap_err2string(err) );
1368 } else if ( err != LDAP_SUCCESS ) {
1369 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1375 write_ldif( LDIF_PUT_VALUE,
1376 "matchedDN", matcheddn, strlen(matcheddn) );
1378 fprintf( stderr, _("Matched DN: %s\n"), matcheddn );
1382 ber_memfree( matcheddn );
1388 write_ldif( LDIF_PUT_TEXT, "text",
1389 text, strlen(text) );
1391 fprintf( stderr, _("Additional information: %s\n"), text );
1395 ber_memfree( text );
1400 for( i=0; refs[i] != NULL; i++ ) {
1402 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1404 fprintf( stderr, _("Referral: %s\n"), refs[i] );
1408 ber_memvfree( (void **) refs );
1412 print_ctrls( ctrls );
1413 ldap_controls_free( ctrls );
1419 static void print_ctrls(
1420 LDAPControl **ctrls )
1423 for(i=0; ctrls[i] != NULL; i++ ) {
1424 /* control: OID criticality base64value */
1425 struct berval *b64 = NULL;
1430 len += strlen( ctrls[i]->ldctl_oid );
1432 /* add enough for space after OID and the critical value itself */
1433 len += ctrls[i]->ldctl_iscritical
1434 ? sizeof("true") : sizeof("false");
1436 /* convert to base64 */
1437 if( ctrls[i]->ldctl_value.bv_len ) {
1438 b64 = ber_memalloc( sizeof(struct berval) );
1440 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1441 ctrls[i]->ldctl_value.bv_len ) + 1;
1442 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1444 b64->bv_len = lutil_b64_ntop(
1445 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1446 ctrls[i]->ldctl_value.bv_len,
1447 b64->bv_val, b64->bv_len );
1451 len += 1 + b64->bv_len;
1454 str = malloc( len + 1 );
1456 strcpy( str, ": " );
1460 strcat( str, ctrls[i]->ldctl_oid );
1461 strcat( str, ctrls[i]->ldctl_iscritical
1462 ? " true" : " false" );
1466 strcat(str, b64->bv_val );
1470 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1471 "control", str, len );
1480 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1484 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1488 fputs( ldif, stdout );
1489 ber_memfree( ldif );
1495 #ifdef LDAP_CONTROL_PAGEDRESULTS
1499 LDAPMessage *result,
1500 struct berval *cookie )
1504 LDAPControl **ctrl = NULL;
1505 LDAPControl *ctrlp = NULL;
1508 struct berval servercookie = { 0, NULL };
1510 rc = ldap_parse_result( ld, result,
1511 &err, NULL, NULL, NULL, &ctrl, 0 );
1513 if( rc != LDAP_SUCCESS ) {
1514 ldap_perror(ld, "ldap_parse_result");
1515 exit( EXIT_FAILURE );
1518 if ( err != LDAP_SUCCESS ) {
1519 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1523 /* Parse the control value
1524 * searchResult ::= SEQUENCE {
1525 * size INTEGER (0..maxInt),
1526 * -- result set size estimate from server - unused
1527 * cookie OCTET STRING
1531 ber = ber_init( &ctrlp->ldctl_value );
1532 if ( ber == NULL ) {
1533 fprintf( stderr, _("Internal error.\n") );
1534 return EXIT_FAILURE;
1537 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1538 ber_dupbv( cookie, &servercookie );
1539 (void) ber_free( ber, 1 );
1541 if( tag == LBER_ERROR ) {
1543 _("Paged results response control could not be decoded.\n") );
1544 return EXIT_FAILURE;
1547 if( entriesLeft < 0 ) {
1549 _("Invalid entries estimate in paged results response.\n") );
1550 return EXIT_FAILURE;
1553 ldap_controls_free( ctrl );
1556 morePagedResults = 0;