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;
54 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
55 " filter\tRFC-2254 compliant LDAP search filter\n"
56 " attributes\twhitespace-separated list of attribute descriptions\n"
57 " which may include:\n"
58 " 1.1 no attributes\n"
59 " * all user attributes\n"
60 " + all operational attributes\n"
63 " -a deref one of never (default), always, search, or find\n"
64 " -A retrieve attribute names only (no values)\n"
65 " -b basedn base dn for search\n"
66 " -E [!]<ctrl>[=<ctrlparam>] search controls (! indicates criticality)\n"
67 " [!]mv=<filter> (matched values filter)\n"
68 " [!]pr=<size> (paged results)\n"
69 #ifdef LDAP_CONTROL_SUBENTRIES
70 " [!]subentries[=true|false] (subentries)\n"
72 #ifdef LDAP_CLIENT_UPDATE
73 " [!]lcup= p/<cint>/<cookie>/<slimit> (client update)\n"
75 * " s/<cint>/<cookie> (client update)\n"
76 * " sp/<cint>/<cookie>/<slimit>\n"
79 " -F prefix URL prefix for files (default: %s)\n"
80 " -l limit time limit (in seconds) for search\n"
81 " -L print responses in LDIFv1 format\n"
82 " -LL print responses in LDIF format without comments\n"
83 " -LLL print responses in LDIF format without comments\n"
85 " -s scope one of base, one, or sub (search scope)\n"
86 " -S attr sort the results by attribute `attr'\n"
87 " -t write binary values to files in temporary directory\n"
88 " -tt write all values to files in temporary directory\n"
89 " -T path write files to directory specified by path (default: %s)\n"
90 " -u include User Friendly entry names in the output\n"
91 " -z limit size limit (in entries) for search\n"
92 , prog, def_urlpre, def_tmpdir );
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_CLIENT_UPDATE
152 static int lcup_cint = 0;
153 static struct berval lcup_cookie = { 0, NULL };
154 static int lcup_slimit = 0;
157 static int pagedResults = 0;
158 static ber_int_t pageSize = 0;
159 static ber_int_t entriesLeft = 0;
160 static ber_int_t morePagedResults = 1;
161 static struct berval cookie = { 0, NULL };
162 static int npagedresponses;
163 static int npagedentries;
164 static int npagedreferences;
165 static int npagedextended;
166 static int npagedpartial;
168 static int parse_page_control(
171 struct berval *cookie );
178 if (*LDAP_DIRSEP != '/') {
179 for (p = url; *p; p++) {
180 if (*p == *LDAP_DIRSEP)
187 const char options[] = "a:Ab:E:F:l:Ls:S:tT:uz:"
188 "Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z";
191 handle_private_option( int i )
194 char *control, *cvalue;
196 case 'a': /* set alias deref option */
197 if ( strcasecmp( optarg, "never" ) == 0 ) {
198 deref = LDAP_DEREF_NEVER;
199 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
200 deref = LDAP_DEREF_SEARCHING;
201 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
202 deref = LDAP_DEREF_FINDING;
203 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
204 deref = LDAP_DEREF_ALWAYS;
206 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
210 case 'A': /* retrieve attribute names only -- no values */
213 case 'b': /* search base */
214 base = strdup( optarg );
216 case 'E': /* search controls */
217 if( version == LDAP_VERSION2 ) {
218 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
220 exit( EXIT_FAILURE );
223 /* should be extended to support comma separated list of
224 * [!]key[=value] parameters, e.g. -E !foo,bar=567
229 if( optarg[0] == '!' ) {
234 control = strdup( optarg );
235 if ( (cvalue = strchr( control, '=' )) != NULL ) {
239 if ( strcasecmp( control, "mv" ) == 0 ) {
240 /* ValuesReturnFilter control */
241 if( valuesReturnFilter ) {
242 fprintf( stderr, "ValuesReturnFilter previously specified\n");
243 exit( EXIT_FAILURE );
245 valuesReturnFilter= 1 + crit;
247 if ( cvalue == NULL ) {
249 "missing filter in ValuesReturnFilter control\n");
250 exit( EXIT_FAILURE );
254 version = LDAP_VERSION3;
256 } else if ( strcasecmp( control, "pr" ) == 0 ) {
258 /* PagedResults control */
259 if ( pagedResults != 0 ) {
260 fprintf( stderr, "PagedResultsControl previously specified\n" );
261 exit( EXIT_FAILURE );
264 num = sscanf( cvalue, "%d", &tmp );
266 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", cvalue);
267 exit( EXIT_FAILURE );
270 pageSize = (ber_int_t) tmp;
271 pagedResults = 1 + crit;
273 #ifdef LDAP_CONTROL_SUBENTRIES
274 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
276 fprintf( stderr, "subentries control previously specified\n");
277 exit( EXIT_FAILURE );
279 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
281 } else if ( strcasecmp( cvalue, "false") == 0 ) {
285 "subentries control value \"%s\" invalid\n",
287 exit( EXIT_FAILURE );
289 if( crit ) subentries *= -1;
292 #ifdef LDAP_CLIENT_UPDATE
293 } else if ( strcasecmp( control, "lcup" ) == 0 ) {
297 fprintf( stderr, "client update control previously specified\n");
298 exit( EXIT_FAILURE );
300 if ( cvalue == NULL ) {
302 "missing specification of client update control\n");
303 exit( EXIT_FAILURE );
305 if ( strncasecmp( cvalue, "p", 1 ) == 0 ) {
306 lcup = LDAP_CUP_PERSIST_ONLY;
308 cookiep = strchr( cvalue, '/' );
310 lcup_cint = atoi( cvalue );
312 slimitp = strchr( cvalue, '/' );
314 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
315 lcup_slimit = atoi( slimitp );
317 } else if ( strncasecmp( cvalue, "s", 1 ) == 0 ) {
318 lcup = LDAP_CUP_SYNC_ONLY;
320 cookiep = strchr( cvalue, '/' );
322 lcup_cint = atoi( cvalue );
323 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
324 } else if ( strncasecmp( cvalue, "sp", 2 ) == 0 ) {
325 lcup = LDAP_CUP_SYNC_AND_PERSIST;
327 cookiep = strchr( cvalue, '/' );
329 lcup_cint = atoi( cvalue );
331 slimitp = strchr( cvalue, '/' );
333 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
334 lcup_slimit = atoi( slimitp );
338 "client update control value \"%s\" invalid\n",
340 exit( EXIT_FAILURE );
342 if ( crit ) lcup *= -1;
346 fprintf( stderr, "Invalid control name: %s\n", control );
350 case 'F': /* uri prefix */
351 if( urlpre ) free( urlpre );
352 urlpre = strdup( optarg );
354 case 'l': /* time limit */
355 timelimit = atoi( optarg );
356 if( timelimit < 0 ) {
357 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
359 exit( EXIT_FAILURE );
362 case 'L': /* print entries in LDIF format */
365 case 's': /* search scope */
366 if ( strcasecmp( optarg, "base" ) == 0 ) {
367 scope = LDAP_SCOPE_BASE;
368 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
369 scope = LDAP_SCOPE_ONELEVEL;
370 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
371 scope = LDAP_SCOPE_SUBTREE;
373 fprintf( stderr, "scope should be base, one, or sub\n" );
377 case 'S': /* sort attribute */
378 sortattr = strdup( optarg );
380 case 't': /* write attribute values to TMPDIR files */
383 case 'T': /* tmpdir */
384 if( tmpdir ) free( tmpdir );
385 tmpdir = strdup( optarg );
387 case 'u': /* include UFN */
390 case 'z': /* size limit */
391 sizelimit = atoi( optarg );
401 private_conn_setup( LDAP *ld )
404 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
406 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
407 exit( EXIT_FAILURE );
409 if (timelimit != -1 &&
410 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
412 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
413 exit( EXIT_FAILURE );
415 if (sizelimit != -1 &&
416 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
418 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
419 exit( EXIT_FAILURE );
425 main( int argc, char **argv )
427 char *filtpattern, **attrs = NULL, line[BUFSIZ];
431 BerElement *seber = NULL, *vrber = NULL, *prber = NULL;
432 #ifdef LDAP_CLIENT_UPDATE
433 BerElement *cuber = NULL;
434 struct berval *cubvalp = NULL;
437 npagedresponses = npagedentries = npagedreferences =
438 npagedextended = npagedpartial = 0;
440 prog = lutil_progname( "ldapsearch", argc, argv );
442 lutil_log_initialize(argc, argv);
444 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
445 (def_tmpdir = getenv("TMP")) == NULL &&
446 (def_tmpdir = getenv("TEMP")) == NULL )
448 def_tmpdir = LDAP_TMPDIR;
452 def_tmpdir = LDAP_TMPDIR;
454 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
456 if( def_urlpre == NULL ) {
461 sprintf( def_urlpre, "file:///%s/",
462 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
464 urlize( def_urlpre );
466 tool_args( argc, argv );
468 if (( argc - optind < 1 ) ||
469 ( *argv[optind] != '(' /*')'*/ &&
470 ( strchr( argv[optind], '=' ) == NULL ) ) )
472 filtpattern = "(objectclass=*)";
474 filtpattern = strdup( argv[optind++] );
477 if ( argv[optind] != NULL ) {
478 attrs = &argv[optind];
481 if ( infile != NULL ) {
482 if ( infile[0] == '-' && infile[1] == '\0' ) {
484 } else if (( fp = fopen( infile, "r" )) == NULL ) {
490 if ( tmpdir == NULL ) {
493 if ( urlpre == NULL )
497 if( urlpre == NULL ) {
498 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
500 if( urlpre == NULL ) {
505 sprintf( urlpre, "file:///%s/",
506 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
514 ld = tool_conn_setup( 0, &private_conn_setup );
516 if ( pw_file || want_bindpw ) {
518 rc = lutil_get_filed_password( pw_file, &passwd );
519 if( rc ) return EXIT_FAILURE;
521 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
522 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
529 if ( manageDSAit || noop || subentries
530 || valuesReturnFilter || pageSize
531 #ifdef LDAP_CLIENT_UPDATE
533 #endif /* LDAP_CLIENT_UPDATE */
539 #ifdef LDAP_CONTROL_SUBENTRIES
541 if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
545 err = ber_printf( seber, "{b}", abs(subentries) == 1 ? 0 : 1 );
547 ber_free( seber, 1 );
548 fprintf( stderr, "Subentries control encoding error!\n" );
552 if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
556 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
557 c[i].ldctl_iscritical = subentries < 1;
562 #ifdef LDAP_CLIENT_UPDATE
564 if (( cuber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
567 err = ber_printf( cuber, "{ei{sO}}", abs(lcup), lcup_cint,
568 LDAP_CLIENT_UPDATE_COOKIE, &lcup_cookie);
569 if ( err == LBER_ERROR ) {
570 ber_free( cuber, 1 );
571 fprintf( stderr, "client update control encoding error!\n" );
575 if ( ber_flatten( cuber, &cubvalp ) == LBER_ERROR ) {
579 c[i].ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE;
580 c[i].ldctl_value=(*cubvalp);
581 c[i].ldctl_iscritical = lcup < 0;
586 if ( valuesReturnFilter ) {
587 if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
591 if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
592 ber_free( vrber, 1 );
593 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
597 if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
601 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
602 c[i].ldctl_iscritical = valuesReturnFilter > 1;
606 if ( pagedResults ) {
607 if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
611 ber_printf( prber, "{iO}", pageSize, &cookie );
612 if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == -1 ) {
616 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
617 c[i].ldctl_iscritical = pagedResults > 1;
621 tool_server_controls( ld, c, i );
623 ber_free( seber, 1 );
624 ber_free( vrber, 1 );
625 ber_free( prber, 1 );
629 fprintf( stderr, "filter%s: %s\nrequesting: ",
630 infile != NULL ? " pattern" : "",
633 if ( attrs == NULL ) {
634 fprintf( stderr, "ALL" );
636 for ( i = 0; attrs[ i ] != NULL; ++i ) {
637 fprintf( stderr, "%s ", attrs[ i ] );
640 fprintf( stderr, "\n" );
644 printf( "# extended LDIF\n" );
645 } else if ( ldif < 3 ) {
646 printf( "version: %d\n\n", 1 );
652 "# base <%s> with scope %s\n"
656 base ? base : "", (scope == LDAP_SCOPE_BASE) ? "base"
657 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"),
658 infile != NULL ? " pattern" : "",
661 if ( attrs == NULL ) {
664 for ( i = 0; attrs[ i ] != NULL; ++i ) {
665 printf( "%s ", attrs[ i ] );
670 printf("\n# with manageDSAit %scontrol",
671 manageDSAit > 1 ? "critical " : "" );
674 printf("\n# with noop %scontrol",
675 noop > 1 ? "critical " : "" );
678 printf("\n# with subentries %scontrol: %s",
679 subentries < 0 ? "critical " : "",
680 abs(subentries) == 1 ? "false" : "true" );
682 if ( valuesReturnFilter ) {
683 printf("\n# with valuesReturnFilter %scontrol: %s",
684 valuesReturnFilter > 1 ? "critical " : "", vrFilter );
687 printf("\n# with pagedResults %scontrol: size=%d",
688 (pagedResults > 1) ? "critical " : "",
695 if ( infile == NULL ) {
696 rc = dosearch( ld, base, scope, NULL, filtpattern,
697 attrs, attrsonly, NULL, NULL, NULL, -1 );
702 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
703 line[ strlen( line ) - 1 ] = '\0';
709 rc = dosearch( ld, base, scope, filtpattern, line,
710 attrs, attrsonly, NULL, NULL, NULL, -1 );
717 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
719 int i, moreEntries, tmpSize;
721 /* Loop to get the next pages when
722 * enter is pressed on the terminal.
724 if ( entriesLeft > 0 ) {
725 printf( "Estimate entries: %d\n", entriesLeft );
727 printf( "Press [size] Enter for the next {%d|size} entries.\n",
730 moreEntries = getchar();
731 while ( moreEntries != EOF && moreEntries != '\n' ) {
732 if ( i < (int)sizeof(buf) - 1 ) {
733 buf[i] = moreEntries;
736 moreEntries = getchar();
740 if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
741 int num = sscanf( buf, "%d", &tmpSize );
743 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", buf);
747 pageSize = (ber_int_t)tmpSize;
766 LDAPControl **sctrls,
767 LDAPControl **cctrls,
768 struct timeval *timeout,
778 LDAPMessage *res, *msg;
781 if( filtpatt != NULL ) {
782 filter = malloc( strlen( filtpatt ) + strlen( value ) );
783 if( filter == NULL ) {
788 sprintf( filter, filtpatt, value );
791 fprintf( stderr, "filter: %s\n", filter );
795 printf( "#\n# filter: %s\n#\n", filter );
806 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
807 sctrls, cctrls, timeout, sizelimit, &msgid );
809 if ( filtpatt != NULL ) {
813 if( rc != LDAP_SUCCESS ) {
814 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
815 prog, ldap_err2string( rc ), rc );
819 nresponses = nentries = nreferences = nextended = npartial = 0;
823 while ((rc = ldap_result( ld, LDAP_RES_ANY,
824 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
828 (void) ldap_sort_entries( ld, &res,
829 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
832 for ( msg = ldap_first_message( ld, res );
834 msg = ldap_next_message( ld, msg ) )
836 if( nresponses++ ) putchar('\n');
838 switch( ldap_msgtype( msg ) ) {
839 case LDAP_RES_SEARCH_ENTRY:
841 print_entry( ld, msg, attrsonly );
844 case LDAP_RES_SEARCH_REFERENCE:
846 print_reference( ld, msg );
849 case LDAP_RES_EXTENDED:
851 print_extended( ld, msg );
853 if( ldap_msgid( msg ) == 0 ) {
854 /* unsolicited extended operation */
859 case LDAP_RES_EXTENDED_PARTIAL:
861 print_partial( ld, msg );
864 case LDAP_RES_SEARCH_RESULT:
865 rc = print_result( ld, msg, 1 );
866 if ( pageSize != 0 ) {
867 rc = parse_page_control( ld, msg, &cookie );
871 #ifdef LDAP_CLIENT_UPDATE
872 if ( nresponses >= lcup_slimit ) {
873 ldap_abandon (ld, ldap_msgid(msg));
884 ldap_perror( ld, "ldap_result" );
889 if ( pageSize != 0 ) {
890 npagedresponses = npagedresponses + nresponses;
891 npagedentries = npagedentries + nentries;
892 npagedreferences = npagedreferences + nreferences;
893 npagedextended = npagedextended + nextended;
894 npagedpartial = npagedpartial + npartial;
895 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
896 printf( "\n# numResponses: %d\n", npagedresponses );
897 if( nentries ) printf( "# numEntries: %d\n", npagedentries );
898 if( nextended ) printf( "# numExtended: %d\n", npagedextended );
899 if( npartial ) printf( "# numPartial: %d\n", npagedpartial );
900 if( nreferences ) printf( "# numReferences: %d\n", npagedreferences );
902 } else if ( ldif < 2 ) {
903 printf( "\n# numResponses: %d\n", nresponses );
904 if( nentries ) printf( "# numEntries: %d\n", nentries );
905 if( nextended ) printf( "# numExtended: %d\n", nextended );
906 if( npartial ) printf( "# numPartial: %d\n", npartial );
907 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
914 /* This is the original version, the old way of doing things. */
922 char tmpfname[ 256 ];
925 BerElement *ber = NULL;
926 struct berval **bvals;
927 LDAPControl **ctrls = NULL;
930 dn = ldap_get_dn( ld, entry );
934 ufn = ldap_dn2ufn( dn );
935 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
937 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
939 rc = ldap_get_entry_controls( ld, entry, &ctrls );
941 if( rc != LDAP_SUCCESS ) {
942 fprintf(stderr, "print_entry: %d\n", rc );
943 ldap_perror( ld, "ldap_get_entry_controls" );
944 exit( EXIT_FAILURE );
948 print_ctrls( ctrls );
949 ldap_controls_free( ctrls );
954 ufn = ldap_dn2ufn( dn );
956 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
959 if( ufn != NULL ) ldap_memfree( ufn );
962 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
963 a = ldap_next_attribute( ld, entry, ber ) )
966 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
968 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
969 for ( i = 0; bvals[i] != NULL; i++ ) {
970 if ( vals2tmp > 1 || ( vals2tmp
971 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
974 /* write value to file */
975 snprintf( tmpfname, sizeof tmpfname,
976 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
980 tmpfd = mkstemp( tmpfname );
987 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
992 if ( fwrite( bvals[ i ]->bv_val,
993 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1002 snprintf( url, sizeof url, "%s%s", urlpre,
1003 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1006 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1009 write_ldif( LDIF_PUT_VALUE, a,
1010 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1013 ber_bvecfree( bvals );
1023 /* This is the proposed new way of doing things.
1024 * It is more efficient, but the API is non-standard.
1033 char tmpfname[ 256 ];
1036 BerElement *ber = NULL;
1037 struct berval bv, *bvals, **bvp = &bvals;
1038 LDAPControl **ctrls = NULL;
1041 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1044 ufn = ldap_dn2ufn( bv.bv_val );
1045 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1047 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1049 rc = ldap_int_get_controls( ber, &ctrls );
1051 if( rc != LDAP_SUCCESS ) {
1052 fprintf(stderr, "print_entry: %d\n", rc );
1053 ldap_perror( ld, "ldap_get_entry_controls" );
1054 exit( EXIT_FAILURE );
1058 print_ctrls( ctrls );
1059 ldap_controls_free( ctrls );
1064 ufn = ldap_dn2ufn( bv.bv_val );
1066 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1069 if( ufn != NULL ) ldap_memfree( ufn );
1071 if ( attrsonly ) bvp = NULL;
1073 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1075 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1077 if (bv.bv_val == NULL) break;
1080 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1083 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1084 if ( vals2tmp > 1 || ( vals2tmp
1085 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1088 /* write value to file */
1089 snprintf( tmpfname, sizeof tmpfname,
1090 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1091 tmpdir, bv.bv_val );
1094 tmpfd = mkstemp( tmpfname );
1101 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1106 if ( fwrite( bvals[ i ].bv_val,
1107 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1116 snprintf( url, sizeof url, "%s%s", urlpre,
1117 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1120 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1123 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1124 bvals[ i ].bv_val, bvals[ i ].bv_len );
1127 ber_memfree( bvals );
1137 static void print_reference(
1139 LDAPMessage *reference )
1143 LDAPControl **ctrls;
1146 printf("# search reference\n");
1149 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1151 if( rc != LDAP_SUCCESS ) {
1152 ldap_perror(ld, "ldap_parse_reference");
1153 exit( EXIT_FAILURE );
1158 for( i=0; refs[i] != NULL; i++ ) {
1159 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1160 "ref", refs[i], strlen(refs[i]) );
1162 ber_memvfree( (void **) refs );
1166 print_ctrls( ctrls );
1167 ldap_controls_free( ctrls );
1171 static void print_extended(
1173 LDAPMessage *extended )
1176 char *retoid = NULL;
1177 struct berval *retdata = NULL;
1180 printf("# extended result response\n");
1183 rc = ldap_parse_extended_result( ld, extended,
1184 &retoid, &retdata, 0 );
1186 if( rc != LDAP_SUCCESS ) {
1187 ldap_perror(ld, "ldap_parse_extended_result");
1188 exit( EXIT_FAILURE );
1192 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1193 "extended", retoid, retoid ? strlen(retoid) : 0 );
1195 ber_memfree( retoid );
1199 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1200 "data", retdata->bv_val, retdata->bv_len );
1202 ber_bvfree( retdata );
1205 print_result( ld, extended, 0 );
1208 static void print_partial(
1210 LDAPMessage *partial )
1213 char *retoid = NULL;
1214 struct berval *retdata = NULL;
1215 LDAPControl **ctrls = NULL;
1218 printf("# extended partial response\n");
1221 rc = ldap_parse_extended_partial( ld, partial,
1222 &retoid, &retdata, &ctrls, 0 );
1224 if( rc != LDAP_SUCCESS ) {
1225 ldap_perror(ld, "ldap_parse_extended_partial");
1226 exit( EXIT_FAILURE );
1230 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1231 "partial", retoid, retoid ? strlen(retoid) : 0 );
1234 ber_memfree( retoid );
1238 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1239 "data", retdata->bv_val, retdata->bv_len );
1242 ber_bvfree( retdata );
1246 print_ctrls( ctrls );
1247 ldap_controls_free( ctrls );
1251 static int print_result(
1253 LDAPMessage *result, int search )
1257 char *matcheddn = NULL;
1260 LDAPControl **ctrls = NULL;
1264 printf("# search result\n");
1267 printf("%s: %d\n", "search", ldap_msgid(result) );
1271 rc = ldap_parse_result( ld, result,
1272 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1274 if( rc != LDAP_SUCCESS ) {
1275 ldap_perror(ld, "ldap_parse_result");
1276 exit( EXIT_FAILURE );
1281 printf( "result: %d %s\n", err, ldap_err2string(err) );
1283 } else if ( err != LDAP_SUCCESS ) {
1284 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1287 if( matcheddn && *matcheddn ) {
1289 write_ldif( LDIF_PUT_VALUE,
1290 "matchedDN", matcheddn, strlen(matcheddn) );
1292 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1295 ber_memfree( matcheddn );
1298 if( text && *text ) {
1300 write_ldif( LDIF_PUT_TEXT, "text",
1301 text, strlen(text) );
1303 fprintf( stderr, "Additional information: %s\n", text );
1306 ber_memfree( text );
1311 for( i=0; refs[i] != NULL; i++ ) {
1313 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1315 fprintf( stderr, "Referral: %s\n", refs[i] );
1319 ber_memvfree( (void **) refs );
1323 print_ctrls( ctrls );
1324 ldap_controls_free( ctrls );
1330 static void print_ctrls(
1331 LDAPControl **ctrls )
1334 for(i=0; ctrls[i] != NULL; i++ ) {
1335 /* control: OID criticality base64value */
1336 struct berval *b64 = NULL;
1341 len += strlen( ctrls[i]->ldctl_oid );
1343 /* add enough for space after OID and the critical value itself */
1344 len += ctrls[i]->ldctl_iscritical
1345 ? sizeof("true") : sizeof("false");
1347 /* convert to base64 */
1348 if( ctrls[i]->ldctl_value.bv_len ) {
1349 b64 = ber_memalloc( sizeof(struct berval) );
1351 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1352 ctrls[i]->ldctl_value.bv_len ) + 1;
1353 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1355 b64->bv_len = lutil_b64_ntop(
1356 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1357 b64->bv_val, b64->bv_len );
1361 len += 1 + b64->bv_len;
1364 str = malloc( len + 1 );
1366 strcpy( str, ": " );
1370 strcat( str, ctrls[i]->ldctl_oid );
1371 strcat( str, ctrls[i]->ldctl_iscritical
1372 ? " true" : " false" );
1376 strcat(str, b64->bv_val );
1380 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1381 "control", str, len );
1390 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1394 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1398 fputs( ldif, stdout );
1399 ber_memfree( ldif );
1408 LDAPMessage *result,
1409 struct berval *cookie )
1413 LDAPControl **ctrl = NULL;
1414 LDAPControl *ctrlp = NULL;
1417 struct berval servercookie = { 0, NULL };
1420 rc = ldap_parse_result( ld, result,
1421 &err, NULL, NULL, NULL, &ctrl, 0 );
1423 if( rc != LDAP_SUCCESS ) {
1424 ldap_perror(ld, "ldap_parse_result");
1425 exit( EXIT_FAILURE );
1428 if ( err != LDAP_SUCCESS ) {
1429 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1433 /* Parse the control value
1434 * searchResult ::= SEQUENCE {
1435 * size INTEGER (0..maxInt),
1436 * -- result set size estimate from server - unused
1437 * cookie OCTET STRING
1440 ber = ber_init( &ctrlp->ldctl_value );
1441 if ( ber == NULL ) {
1442 fprintf( stderr, "Internal error.\n" );
1443 return EXIT_FAILURE;
1446 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1447 ber_dupbv( cookie, &servercookie );
1448 (void) ber_free( ber, 1 );
1450 if( tag == LBER_ERROR ) {
1451 fprintf( stderr, "Paged results response control could not be decoded.\n" );
1452 return EXIT_FAILURE;
1455 if( entriesLeft < 0 ) {
1456 fprintf( stderr, "Invalid entries estimate in paged results response.\n" );
1457 return EXIT_FAILURE;
1461 ldap_controls_free( ctrl );
1463 morePagedResults = 0;