3 * Copyright 1998-2002 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 #ifndef LDAP_CLIENT_UPDATE
530 if ( manageDSAit || noop || subentries
531 || valuesReturnFilter || pageSize )
533 if ( manageDSAit || noop || subentries
534 || valuesReturnFilter || pageSize || lcup )
541 #ifdef LDAP_CONTROL_SUBENTRIES
543 if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
547 err = ber_printf( seber, "{b}", abs(subentries) == 1 ? 0 : 1 );
548 if ( err == LBER_ERROR ) {
549 ber_free( seber, 1 );
550 fprintf( stderr, "Subentries control encoding error!\n" );
554 if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
558 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
559 c[i].ldctl_iscritical = subentries < 1;
564 #ifdef LDAP_CLIENT_UPDATE
566 if (( cuber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
569 err = ber_printf( cuber, "{ei{sO}}", abs(lcup), lcup_cint,
570 LDAP_CLIENT_UPDATE_COOKIE, &lcup_cookie);
571 if ( err == LBER_ERROR ) {
572 ber_free( cuber, 1 );
573 fprintf( stderr, "client update control encoding error!\n" );
577 if ( ber_flatten( cuber, &cubvalp ) == LBER_ERROR ) {
581 c[i].ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE;
582 c[i].ldctl_value=(*cubvalp);
583 c[i].ldctl_iscritical = lcup < 0;
588 if ( valuesReturnFilter ) {
589 if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
593 if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
594 ber_free( vrber, 1 );
595 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
599 if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
603 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
604 c[i].ldctl_iscritical = valuesReturnFilter > 1;
608 if ( pagedResults ) {
609 if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
613 ber_printf( prber, "{iO}", pageSize, &cookie );
614 if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == LBER_ERROR ) {
618 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
619 c[i].ldctl_iscritical = pagedResults > 1;
623 tool_server_controls( ld, c, i );
625 ber_free( seber, 1 );
626 ber_free( vrber, 1 );
627 ber_free( prber, 1 );
631 fprintf( stderr, "filter%s: %s\nrequesting: ",
632 infile != NULL ? " pattern" : "",
635 if ( attrs == NULL ) {
636 fprintf( stderr, "ALL" );
638 for ( i = 0; attrs[ i ] != NULL; ++i ) {
639 fprintf( stderr, "%s ", attrs[ i ] );
642 fprintf( stderr, "\n" );
646 printf( "# extended LDIF\n" );
647 } else if ( ldif < 3 ) {
648 printf( "version: %d\n\n", 1 );
654 "# base <%s> with scope %s\n"
658 base ? base : "", (scope == LDAP_SCOPE_BASE) ? "base"
659 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"),
660 infile != NULL ? " pattern" : "",
663 if ( attrs == NULL ) {
666 for ( i = 0; attrs[ i ] != NULL; ++i ) {
667 printf( "%s ", attrs[ i ] );
672 printf("\n# with manageDSAit %scontrol",
673 manageDSAit > 1 ? "critical " : "" );
676 printf("\n# with noop %scontrol",
677 noop > 1 ? "critical " : "" );
680 printf("\n# with subentries %scontrol: %s",
681 subentries < 0 ? "critical " : "",
682 abs(subentries) == 1 ? "false" : "true" );
684 if ( valuesReturnFilter ) {
685 printf("\n# with valuesReturnFilter %scontrol: %s",
686 valuesReturnFilter > 1 ? "critical " : "", vrFilter );
689 printf("\n# with pagedResults %scontrol: size=%d",
690 (pagedResults > 1) ? "critical " : "",
697 if ( infile == NULL ) {
698 rc = dosearch( ld, base, scope, NULL, filtpattern,
699 attrs, attrsonly, NULL, NULL, NULL, -1 );
704 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
705 line[ strlen( line ) - 1 ] = '\0';
711 rc = dosearch( ld, base, scope, filtpattern, line,
712 attrs, attrsonly, NULL, NULL, NULL, -1 );
719 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
721 int i, moreEntries, tmpSize;
723 /* Loop to get the next pages when
724 * enter is pressed on the terminal.
726 if ( entriesLeft > 0 ) {
727 printf( "Estimate entries: %d\n", entriesLeft );
729 printf( "Press [size] Enter for the next {%d|size} entries.\n",
732 moreEntries = getchar();
733 while ( moreEntries != EOF && moreEntries != '\n' ) {
734 if ( i < sizeof(buf) - 1 ) {
735 buf[i] = moreEntries;
738 moreEntries = getchar();
742 if ( i > 0 && isdigit( buf[0] ) ) {
743 int num = sscanf( buf, "%d", &tmpSize );
745 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", buf);
749 pageSize = (ber_int_t)tmpSize;
768 LDAPControl **sctrls,
769 LDAPControl **cctrls,
770 struct timeval *timeout,
780 LDAPMessage *res, *msg;
783 if( filtpatt != NULL ) {
784 filter = malloc( strlen( filtpatt ) + strlen( value ) );
785 if( filter == NULL ) {
790 sprintf( filter, filtpatt, value );
793 fprintf( stderr, "filter: %s\n", filter );
797 printf( "#\n# filter: %s\n#\n", filter );
808 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
809 sctrls, cctrls, timeout, sizelimit, &msgid );
811 if ( filtpatt != NULL ) {
815 if( rc != LDAP_SUCCESS ) {
816 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
817 prog, ldap_err2string( rc ), rc );
821 nresponses = nentries = nreferences = nextended = npartial = 0;
825 while ((rc = ldap_result( ld, LDAP_RES_ANY,
826 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
830 (void) ldap_sort_entries( ld, &res,
831 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
834 for ( msg = ldap_first_message( ld, res );
836 msg = ldap_next_message( ld, msg ) )
838 if( nresponses++ ) putchar('\n');
840 switch( ldap_msgtype( msg ) ) {
841 case LDAP_RES_SEARCH_ENTRY:
843 print_entry( ld, msg, attrsonly );
846 case LDAP_RES_SEARCH_REFERENCE:
848 print_reference( ld, msg );
851 case LDAP_RES_EXTENDED:
853 print_extended( ld, msg );
855 if( ldap_msgid( msg ) == 0 ) {
856 /* unsolicited extended operation */
861 case LDAP_RES_EXTENDED_PARTIAL:
863 print_partial( ld, msg );
866 case LDAP_RES_SEARCH_RESULT:
867 rc = print_result( ld, msg, 1 );
868 if ( pageSize != 0 ) {
869 rc = parse_page_control( ld, msg, &cookie );
873 #ifdef LDAP_CLIENT_UPDATE
874 if ( nresponses >= lcup_slimit ) {
875 ldap_abandon (ld, ldap_msgid(msg));
886 ldap_perror( ld, "ldap_result" );
891 if ( pageSize != 0 ) {
892 npagedresponses = npagedresponses + nresponses;
893 npagedentries = npagedentries + nentries;
894 npagedreferences = npagedreferences + nreferences;
895 npagedextended = npagedextended + nextended;
896 npagedpartial = npagedpartial + npartial;
897 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
898 printf( "\n# numResponses: %d\n", npagedresponses );
899 if( nentries ) printf( "# numEntries: %d\n", npagedentries );
900 if( nextended ) printf( "# numExtended: %d\n", npagedextended );
901 if( npartial ) printf( "# numPartial: %d\n", npagedpartial );
902 if( nreferences ) printf( "# numReferences: %d\n", npagedreferences );
904 } else if ( ldif < 2 ) {
905 printf( "\n# numResponses: %d\n", nresponses );
906 if( nentries ) printf( "# numEntries: %d\n", nentries );
907 if( nextended ) printf( "# numExtended: %d\n", nextended );
908 if( npartial ) printf( "# numPartial: %d\n", npartial );
909 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
916 /* This is the original version, the old way of doing things. */
924 char tmpfname[ 256 ];
927 BerElement *ber = NULL;
928 struct berval **bvals;
929 LDAPControl **ctrls = NULL;
932 dn = ldap_get_dn( ld, entry );
936 ufn = ldap_dn2ufn( dn );
937 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
939 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
941 rc = ldap_get_entry_controls( ld, entry, &ctrls );
943 if( rc != LDAP_SUCCESS ) {
944 fprintf(stderr, "print_entry: %d\n", rc );
945 ldap_perror( ld, "ldap_get_entry_controls" );
946 exit( EXIT_FAILURE );
950 print_ctrls( ctrls );
951 ldap_controls_free( ctrls );
956 ufn = ldap_dn2ufn( dn );
958 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
961 if( ufn != NULL ) ldap_memfree( ufn );
964 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
965 a = ldap_next_attribute( ld, entry, ber ) )
968 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
970 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
971 for ( i = 0; bvals[i] != NULL; i++ ) {
972 if ( vals2tmp > 1 || ( vals2tmp
973 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
976 /* write value to file */
977 snprintf( tmpfname, sizeof tmpfname,
978 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
982 tmpfd = mkstemp( tmpfname );
989 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
994 if ( fwrite( bvals[ i ]->bv_val,
995 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1004 snprintf( url, sizeof url, "%s%s", urlpre,
1005 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1008 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1011 write_ldif( LDIF_PUT_VALUE, a,
1012 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1015 ber_bvecfree( bvals );
1025 /* This is the proposed new way of doing things.
1026 * It is more efficient, but the API is non-standard.
1035 char tmpfname[ 256 ];
1038 BerElement *ber = NULL;
1039 struct berval bv, *bvals, **bvp = &bvals;
1040 LDAPControl **ctrls = NULL;
1043 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1046 ufn = ldap_dn2ufn( bv.bv_val );
1047 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1049 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1051 rc = ldap_int_get_controls( ber, &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( bv.bv_val );
1068 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1071 if( ufn != NULL ) ldap_memfree( ufn );
1073 if ( attrsonly ) bvp = NULL;
1075 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1077 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1079 if (bv.bv_val == NULL) break;
1082 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1085 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1086 if ( vals2tmp > 1 || ( vals2tmp
1087 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1090 /* write value to file */
1091 snprintf( tmpfname, sizeof tmpfname,
1092 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1093 tmpdir, bv.bv_val );
1096 tmpfd = mkstemp( tmpfname );
1103 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1108 if ( fwrite( bvals[ i ].bv_val,
1109 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1118 snprintf( url, sizeof url, "%s%s", urlpre,
1119 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1122 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1125 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1126 bvals[ i ].bv_val, bvals[ i ].bv_len );
1129 ber_memfree( bvals );
1139 static void print_reference(
1141 LDAPMessage *reference )
1145 LDAPControl **ctrls;
1148 printf("# search reference\n");
1151 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1153 if( rc != LDAP_SUCCESS ) {
1154 ldap_perror(ld, "ldap_parse_reference");
1155 exit( EXIT_FAILURE );
1160 for( i=0; refs[i] != NULL; i++ ) {
1161 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1162 "ref", refs[i], strlen(refs[i]) );
1164 ber_memvfree( (void **) refs );
1168 print_ctrls( ctrls );
1169 ldap_controls_free( ctrls );
1173 static void print_extended(
1175 LDAPMessage *extended )
1178 char *retoid = NULL;
1179 struct berval *retdata = NULL;
1182 printf("# extended result response\n");
1185 rc = ldap_parse_extended_result( ld, extended,
1186 &retoid, &retdata, 0 );
1188 if( rc != LDAP_SUCCESS ) {
1189 ldap_perror(ld, "ldap_parse_extended_result");
1190 exit( EXIT_FAILURE );
1194 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1195 "extended", retoid, retoid ? strlen(retoid) : 0 );
1197 ber_memfree( retoid );
1201 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1202 "data", retdata->bv_val, retdata->bv_len );
1204 ber_bvfree( retdata );
1207 print_result( ld, extended, 0 );
1210 static void print_partial(
1212 LDAPMessage *partial )
1215 char *retoid = NULL;
1216 struct berval *retdata = NULL;
1217 LDAPControl **ctrls = NULL;
1220 printf("# extended partial response\n");
1223 rc = ldap_parse_extended_partial( ld, partial,
1224 &retoid, &retdata, &ctrls, 0 );
1226 if( rc != LDAP_SUCCESS ) {
1227 ldap_perror(ld, "ldap_parse_extended_partial");
1228 exit( EXIT_FAILURE );
1232 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1233 "partial", retoid, retoid ? strlen(retoid) : 0 );
1236 ber_memfree( retoid );
1240 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1241 "data", retdata->bv_val, retdata->bv_len );
1244 ber_bvfree( retdata );
1248 print_ctrls( ctrls );
1249 ldap_controls_free( ctrls );
1253 static int print_result(
1255 LDAPMessage *result, int search )
1259 char *matcheddn = NULL;
1262 LDAPControl **ctrls = NULL;
1266 printf("# search result\n");
1269 printf("%s: %d\n", "search", ldap_msgid(result) );
1273 rc = ldap_parse_result( ld, result,
1274 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1276 if( rc != LDAP_SUCCESS ) {
1277 ldap_perror(ld, "ldap_parse_result");
1278 exit( EXIT_FAILURE );
1283 printf( "result: %d %s\n", err, ldap_err2string(err) );
1285 } else if ( err != LDAP_SUCCESS ) {
1286 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1289 if( matcheddn && *matcheddn ) {
1291 write_ldif( LDIF_PUT_VALUE,
1292 "matchedDN", matcheddn, strlen(matcheddn) );
1294 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1297 ber_memfree( matcheddn );
1300 if( text && *text ) {
1302 write_ldif( LDIF_PUT_TEXT, "text",
1303 text, strlen(text) );
1305 fprintf( stderr, "Additional information: %s\n", text );
1308 ber_memfree( text );
1313 for( i=0; refs[i] != NULL; i++ ) {
1315 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1317 fprintf( stderr, "Referral: %s\n", refs[i] );
1321 ber_memvfree( (void **) refs );
1325 print_ctrls( ctrls );
1326 ldap_controls_free( ctrls );
1332 static void print_ctrls(
1333 LDAPControl **ctrls )
1336 for(i=0; ctrls[i] != NULL; i++ ) {
1337 /* control: OID criticality base64value */
1338 struct berval *b64 = NULL;
1343 len += strlen( ctrls[i]->ldctl_oid );
1345 /* add enough for space after OID and the critical value itself */
1346 len += ctrls[i]->ldctl_iscritical
1347 ? sizeof("true") : sizeof("false");
1349 /* convert to base64 */
1350 if( ctrls[i]->ldctl_value.bv_len ) {
1351 b64 = ber_memalloc( sizeof(struct berval) );
1353 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1354 ctrls[i]->ldctl_value.bv_len ) + 1;
1355 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1357 b64->bv_len = lutil_b64_ntop(
1358 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1359 b64->bv_val, b64->bv_len );
1363 len += 1 + b64->bv_len;
1366 str = malloc( len + 1 );
1368 strcpy( str, ": " );
1372 strcat( str, ctrls[i]->ldctl_oid );
1373 strcat( str, ctrls[i]->ldctl_iscritical
1374 ? " true" : " false" );
1378 strcat(str, b64->bv_val );
1382 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1383 "control", str, len );
1392 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1396 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1400 fputs( ldif, stdout );
1401 ber_memfree( ldif );
1410 LDAPMessage *result,
1411 struct berval *cookie )
1415 LDAPControl **ctrl = NULL;
1416 LDAPControl *ctrlp = NULL;
1419 struct berval servercookie = { 0, NULL };
1422 rc = ldap_parse_result( ld, result,
1423 &err, NULL, NULL, NULL, &ctrl, 0 );
1425 if( rc != LDAP_SUCCESS ) {
1426 ldap_perror(ld, "ldap_parse_result");
1427 exit( EXIT_FAILURE );
1430 if ( err != LDAP_SUCCESS ) {
1431 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1435 /* Parse the control value
1436 * searchResult ::= SEQUENCE {
1437 * size INTEGER (0..maxInt),
1438 * -- result set size estimate from server - unused
1439 * cookie OCTET STRING
1442 ber = ber_init( &ctrlp->ldctl_value );
1443 if ( ber == NULL ) {
1444 fprintf( stderr, "Internal error.\n" );
1445 return EXIT_FAILURE;
1448 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1449 ber_dupbv( cookie, &servercookie );
1450 (void) ber_free( ber, 1 );
1452 if( tag == LBER_ERROR ) {
1453 fprintf( stderr, "Paged results response control could not be decoded.\n" );
1454 return EXIT_FAILURE;
1457 if( entriesLeft < 0 ) {
1458 fprintf( stderr, "Invalid entries estimate in paged results response.\n" );
1459 return EXIT_FAILURE;
1463 ldap_controls_free( ctrl );
1465 morePagedResults = 0;