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"
80 " [!]sync= ro[/<cookie>] (ldap sync - refreshOnly)\n"
81 " rp[/<cookie>][/<slimit>] (ldap sync - refreshAndPersist)\n"
83 " -F prefix URL prefix for files (default: %s)\n"
84 " -l limit time limit (in seconds) for search\n"
85 " -L print responses in LDIFv1 format\n"
86 " -LL print responses in LDIF format without comments\n"
87 " -LLL print responses in LDIF format without comments\n"
89 " -s scope one of base, one, or sub (search scope)\n"
90 " -S attr sort the results by attribute `attr'\n"
91 " -t write binary values to files in temporary directory\n"
92 " -tt write all values to files in temporary directory\n"
93 " -T path write files to directory specified by path (default: %s)\n"
94 " -u include User Friendly entry names in the output\n"
95 " -z limit size limit (in entries) for search\n"
96 , prog, def_urlpre, def_tmpdir );
101 static void print_entry LDAP_P((
106 static void print_reference(
108 LDAPMessage *reference );
110 static void print_extended(
112 LDAPMessage *extended );
114 static void print_partial(
116 LDAPMessage *partial );
118 static int print_result(
123 static void print_ctrls(
124 LDAPControl **ctrls );
126 static int write_ldif LDAP_P((
132 static int dosearch LDAP_P((
140 LDAPControl **sctrls,
141 LDAPControl **cctrls,
142 struct timeval *timeout,
145 static char *tmpdir = NULL;
146 static char *urlpre = NULL;
147 static char *base = NULL;
148 static char *sortattr = NULL;
149 static int includeufn, vals2tmp = 0, ldif = 0;
151 static int subentries = 0, valuesReturnFilter = 0;
152 static char *vrFilter = NULL;
154 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
156 static int ldapsync = 0;
159 #ifdef LDAP_CLIENT_UPDATE
160 static int lcup_cint = 0;
161 static struct berval lcup_cookie = { 0, NULL };
162 static int lcup_slimit = -1;
166 static struct berval sync_cookie = { 0, NULL };
167 static int sync_slimit = -1;
170 static int pagedResults = 0;
171 static ber_int_t pageSize = 0;
172 static ber_int_t entriesLeft = 0;
173 static ber_int_t morePagedResults = 1;
174 static struct berval cookie = { 0, NULL };
175 static int npagedresponses;
176 static int npagedentries;
177 static int npagedreferences;
178 static int npagedextended;
179 static int npagedpartial;
181 static int parse_page_control(
184 struct berval *cookie );
191 if (*LDAP_DIRSEP != '/') {
192 for (p = url; *p; p++) {
193 if (*p == *LDAP_DIRSEP)
200 const char options[] = "a:Ab:E:F:l:Ls:S:tT:uz:"
201 "Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
204 handle_private_option( int i )
207 char *control, *cvalue;
209 case 'a': /* set alias deref option */
210 if ( strcasecmp( optarg, "never" ) == 0 ) {
211 deref = LDAP_DEREF_NEVER;
212 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
213 deref = LDAP_DEREF_SEARCHING;
214 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
215 deref = LDAP_DEREF_FINDING;
216 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
217 deref = LDAP_DEREF_ALWAYS;
219 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
223 case 'A': /* retrieve attribute names only -- no values */
226 case 'b': /* search base */
227 base = strdup( optarg );
229 case 'E': /* search controls */
230 if( protocol == LDAP_VERSION2 ) {
231 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
233 exit( EXIT_FAILURE );
236 /* should be extended to support comma separated list of
237 * [!]key[=value] parameters, e.g. -E !foo,bar=567
242 if( optarg[0] == '!' ) {
247 control = strdup( optarg );
248 if ( (cvalue = strchr( control, '=' )) != NULL ) {
252 if ( strcasecmp( control, "mv" ) == 0 ) {
253 /* ValuesReturnFilter control */
254 if( valuesReturnFilter ) {
255 fprintf( stderr, "ValuesReturnFilter previously specified\n");
256 exit( EXIT_FAILURE );
258 valuesReturnFilter= 1 + crit;
260 if ( cvalue == NULL ) {
262 "missing filter in ValuesReturnFilter control\n");
263 exit( EXIT_FAILURE );
267 protocol = LDAP_VERSION3;
269 } else if ( strcasecmp( control, "pr" ) == 0 ) {
271 /* PagedResults control */
272 if ( pagedResults != 0 ) {
273 fprintf( stderr, "PagedResultsControl previously specified\n" );
274 exit( EXIT_FAILURE );
277 num = sscanf( cvalue, "%d", &tmp );
279 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", cvalue);
280 exit( EXIT_FAILURE );
283 pageSize = (ber_int_t) tmp;
284 pagedResults = 1 + crit;
286 #ifdef LDAP_CONTROL_SUBENTRIES
287 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
289 fprintf( stderr, "subentries control previously specified\n");
290 exit( EXIT_FAILURE );
292 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
294 } else if ( strcasecmp( cvalue, "false") == 0 ) {
298 "subentries control value \"%s\" invalid\n",
300 exit( EXIT_FAILURE );
302 if( crit ) subentries *= -1;
305 #ifdef LDAP_CLIENT_UPDATE
306 } else if ( strcasecmp( control, "lcup" ) == 0 ) {
310 fprintf( stderr, "client update control previously specified\n");
311 exit( EXIT_FAILURE );
313 if ( ldapsync != -1 ) {
314 fprintf( stderr, "ldap sync control previously specified\n");
315 exit( EXIT_FAILURE );
317 if ( cvalue == NULL ) {
319 "missing specification of client update control\n");
320 exit( EXIT_FAILURE );
322 if ( strncasecmp( cvalue, "p", 1 ) == 0 ) {
323 lcup = LDAP_CUP_PERSIST_ONLY;
324 cvalue = strchr( cvalue, '/' );
326 cookiep = strchr( cvalue, '/' );
328 lcup_cint = atoi( cvalue );
330 slimitp = strchr( cvalue, '/' );
332 while ( isspace( *cookiep ) ) cookiep++;
333 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
334 lcup_slimit = atoi( slimitp );
336 } else if ( strncasecmp( cvalue, "s", 1 ) == 0 ) {
337 lcup = LDAP_CUP_SYNC_ONLY;
339 cookiep = strchr( cvalue, '/' );
341 lcup_cint = atoi( cvalue );
342 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
343 } else if ( strncasecmp( cvalue, "sp", 2 ) == 0 ) {
344 lcup = LDAP_CUP_SYNC_AND_PERSIST;
346 cookiep = strchr( cvalue, '/' );
348 lcup_cint = atoi( cvalue );
350 slimitp = strchr( cvalue, '/' );
352 ber_str2bv( cookiep, 0, 0, &lcup_cookie );
353 lcup_slimit = atoi( slimitp );
357 "client update control value \"%s\" invalid\n",
359 exit( EXIT_FAILURE );
361 if ( crit ) lcup *= -1;
365 } else if ( strcasecmp( control, "sync" ) == 0 ) {
369 fprintf( stderr, "ldap sync control previously specified\n" );
370 exit( EXIT_FAILURE );
373 fprintf( stderr, "client update control previously specified\n" );
374 exit( EXIT_FAILURE );
376 if ( cvalue == NULL ) {
378 "missing specification of ldap sync control\n");
379 exit( EXIT_FAILURE );
381 if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
382 ldapsync = LDAP_SYNC_REFRESH_ONLY;
383 cookiep = strchr( cvalue, '/' );
384 if ( cookiep != NULL ) {
386 if ( *cookiep != '\0' ) {
387 ber_str2bv( cookiep, 0, 0, &sync_cookie );
390 } else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
391 ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
392 cookiep = strchr( cvalue, '/' );
393 if ( cookiep != NULL ) {
397 slimitp = strchr( cvalue, '/' );
398 if ( slimitp != NULL ) {
401 if ( cookiep != NULL && *cookiep != '\0' )
402 ber_str2bv( cookiep, 0, 0, &sync_cookie );
403 if ( slimitp != NULL && *slimitp != '\0' )
404 sync_slimit = atoi( slimitp );
407 "ldap sync control value \"%s\" invalid\n",
409 exit( EXIT_FAILURE );
411 if ( crit ) ldapsync *= -1;
415 fprintf( stderr, "Invalid control name: %s\n", control );
419 case 'F': /* uri prefix */
420 if( urlpre ) free( urlpre );
421 urlpre = strdup( optarg );
423 case 'l': /* time limit */
424 timelimit = atoi( optarg );
425 if( timelimit < 0 ) {
426 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
428 exit( EXIT_FAILURE );
431 case 'L': /* print entries in LDIF format */
434 case 's': /* search scope */
435 if ( strcasecmp( optarg, "base" ) == 0 ) {
436 scope = LDAP_SCOPE_BASE;
437 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
438 scope = LDAP_SCOPE_ONELEVEL;
439 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
440 scope = LDAP_SCOPE_SUBTREE;
442 fprintf( stderr, "scope should be base, one, or sub\n" );
446 case 'S': /* sort attribute */
447 sortattr = strdup( optarg );
449 case 't': /* write attribute values to TMPDIR files */
452 case 'T': /* tmpdir */
453 if( tmpdir ) free( tmpdir );
454 tmpdir = strdup( optarg );
456 case 'u': /* include UFN */
459 case 'z': /* size limit */
460 sizelimit = atoi( optarg );
470 private_conn_setup( LDAP *ld )
473 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
475 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
476 exit( EXIT_FAILURE );
478 if (timelimit != -1 &&
479 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
481 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
482 exit( EXIT_FAILURE );
484 if (sizelimit != -1 &&
485 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
487 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
488 exit( EXIT_FAILURE );
494 main( int argc, char **argv )
496 char *filtpattern, **attrs = NULL, line[BUFSIZ];
500 BerElement *seber = NULL, *vrber = NULL, *prber = NULL;
501 #ifdef LDAP_CLIENT_UPDATE
502 BerElement *cuber = NULL;
503 struct berval *cubvalp = NULL;
507 BerElement *syncber = NULL;
508 struct berval *syncbvalp = NULL;
511 npagedresponses = npagedentries = npagedreferences =
512 npagedextended = npagedpartial = 0;
514 prog = lutil_progname( "ldapsearch", argc, argv );
516 lutil_log_initialize(argc, argv);
518 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
519 (def_tmpdir = getenv("TMP")) == NULL &&
520 (def_tmpdir = getenv("TEMP")) == NULL )
522 def_tmpdir = LDAP_TMPDIR;
526 def_tmpdir = LDAP_TMPDIR;
528 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
530 if( def_urlpre == NULL ) {
535 sprintf( def_urlpre, "file:///%s/",
536 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
538 urlize( def_urlpre );
540 tool_args( argc, argv );
542 if (( argc - optind < 1 ) ||
543 ( *argv[optind] != '(' /*')'*/ &&
544 ( strchr( argv[optind], '=' ) == NULL ) ) )
546 filtpattern = "(objectclass=*)";
548 filtpattern = strdup( argv[optind++] );
551 if ( argv[optind] != NULL ) {
552 attrs = &argv[optind];
555 if ( infile != NULL ) {
556 if ( infile[0] == '-' && infile[1] == '\0' ) {
558 } else if (( fp = fopen( infile, "r" )) == NULL ) {
564 if ( tmpdir == NULL ) {
567 if ( urlpre == NULL )
571 if( urlpre == NULL ) {
572 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
574 if( urlpre == NULL ) {
579 sprintf( urlpre, "file:///%s/",
580 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
588 ld = tool_conn_setup( 0, &private_conn_setup );
590 if ( pw_file || want_bindpw ) {
592 rc = lutil_get_filed_password( pw_file, &passwd );
593 if( rc ) return EXIT_FAILURE;
595 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
596 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
603 if ( manageDSAit || noop || subentries
604 || valuesReturnFilter || pageSize
605 #ifdef LDAP_CLIENT_UPDATE
616 #ifdef LDAP_CONTROL_SUBENTRIES
618 if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
622 err = ber_printf( seber, "{b}", abs(subentries) == 1 ? 0 : 1 );
624 ber_free( seber, 1 );
625 fprintf( stderr, "Subentries control encoding error!\n" );
629 if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
633 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
634 c[i].ldctl_iscritical = subentries < 1;
639 #ifdef LDAP_CLIENT_UPDATE
641 if (( cuber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
645 if ( lcup_cookie.bv_len == 0 ) {
646 err = ber_printf( cuber, "{ei}", abs(lcup), lcup_cint );
648 err = ber_printf( cuber, "{ei{sO}}", abs(lcup), lcup_cint,
649 LDAP_LCUP_COOKIE_OID, &lcup_cookie );
652 if ( err == LBER_ERROR ) {
653 ber_free( cuber, 1 );
654 fprintf( stderr, "client update control encoding error!\n" );
658 if ( ber_flatten( cuber, &cubvalp ) == LBER_ERROR ) {
662 c[i].ldctl_oid = LDAP_CONTROL_CLIENT_UPDATE;
663 c[i].ldctl_value = (*cubvalp);
664 c[i].ldctl_iscritical = lcup < 0;
671 if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
675 if ( sync_cookie.bv_len == 0 ) {
676 err = ber_printf( syncber, "{e}", abs(ldapsync) );
678 err = ber_printf( syncber, "{eO}", abs(ldapsync),
682 if ( err == LBER_ERROR ) {
683 ber_free( syncber, 1 );
684 fprintf( stderr, "ldap sync control encoding error!\n" );
688 if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
692 c[i].ldctl_oid = LDAP_CONTROL_SYNC;
693 c[i].ldctl_value = (*syncbvalp);
694 c[i].ldctl_iscritical = ldapsync < 0;
699 if ( valuesReturnFilter ) {
700 if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
704 if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
705 ber_free( vrber, 1 );
706 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
710 if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
714 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
715 c[i].ldctl_iscritical = valuesReturnFilter > 1;
719 if ( pagedResults ) {
720 if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
724 ber_printf( prber, "{iO}", pageSize, &cookie );
725 if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == -1 ) {
729 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
730 c[i].ldctl_iscritical = pagedResults > 1;
734 tool_server_controls( ld, c, i );
736 ber_free( seber, 1 );
737 ber_free( vrber, 1 );
738 ber_free( prber, 1 );
742 fprintf( stderr, "filter%s: %s\nrequesting: ",
743 infile != NULL ? " pattern" : "",
746 if ( attrs == NULL ) {
747 fprintf( stderr, "ALL" );
749 for ( i = 0; attrs[ i ] != NULL; ++i ) {
750 fprintf( stderr, "%s ", attrs[ i ] );
753 fprintf( stderr, "\n" );
757 printf( "# extended LDIF\n" );
758 } else if ( ldif < 3 ) {
759 printf( "version: %d\n\n", 1 );
765 "# base <%s> with scope %s\n"
769 base ? base : "", (scope == LDAP_SCOPE_BASE) ? "base"
770 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"),
771 infile != NULL ? " pattern" : "",
774 if ( attrs == NULL ) {
777 for ( i = 0; attrs[ i ] != NULL; ++i ) {
778 printf( "%s ", attrs[ i ] );
783 printf("\n# with manageDSAit %scontrol",
784 manageDSAit > 1 ? "critical " : "" );
787 printf("\n# with noop %scontrol",
788 noop > 1 ? "critical " : "" );
791 printf("\n# with subentries %scontrol: %s",
792 subentries < 0 ? "critical " : "",
793 abs(subentries) == 1 ? "false" : "true" );
795 if ( valuesReturnFilter ) {
796 printf("\n# with valuesReturnFilter %scontrol: %s",
797 valuesReturnFilter > 1 ? "critical " : "", vrFilter );
800 printf("\n# with pagedResults %scontrol: size=%d",
801 (pagedResults > 1) ? "critical " : "",
808 if ( infile == NULL ) {
809 rc = dosearch( ld, base, scope, NULL, filtpattern,
810 attrs, attrsonly, NULL, NULL, NULL, -1 );
815 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
816 line[ strlen( line ) - 1 ] = '\0';
822 rc = dosearch( ld, base, scope, filtpattern, line,
823 attrs, attrsonly, NULL, NULL, NULL, -1 );
830 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
832 int i, moreEntries, tmpSize;
834 /* Loop to get the next pages when
835 * enter is pressed on the terminal.
837 if ( entriesLeft > 0 ) {
838 printf( "Estimate entries: %d\n", entriesLeft );
840 printf( "Press [size] Enter for the next {%d|size} entries.\n",
843 moreEntries = getchar();
844 while ( moreEntries != EOF && moreEntries != '\n' ) {
845 if ( i < (int)sizeof(buf) - 1 ) {
846 buf[i] = moreEntries;
849 moreEntries = getchar();
853 if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
854 int num = sscanf( buf, "%d", &tmpSize );
856 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", buf);
860 pageSize = (ber_int_t)tmpSize;
879 LDAPControl **sctrls,
880 LDAPControl **cctrls,
881 struct timeval *timeout,
891 LDAPMessage *res, *msg;
895 struct berval *retdata = NULL;
896 int nresponses_psearch = -1;
897 int cancel_msgid = -1;
900 if( filtpatt != NULL ) {
901 filter = malloc( strlen( filtpatt ) + strlen( value ) );
902 if( filter == NULL ) {
907 sprintf( filter, filtpatt, value );
910 fprintf( stderr, "filter: %s\n", filter );
914 printf( "#\n# filter: %s\n#\n", filter );
925 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
926 sctrls, cctrls, timeout, sizelimit, &msgid );
928 printf("msgid = %d\n", msgid);
930 if ( filtpatt != NULL ) {
934 if( rc != LDAP_SUCCESS ) {
935 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
936 prog, ldap_err2string( rc ), rc );
940 nresponses = nentries = nreferences = nextended = npartial = 0;
944 while ((rc = ldap_result( ld, LDAP_RES_ANY,
945 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
949 (void) ldap_sort_entries( ld, &res,
950 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
953 for ( msg = ldap_first_message( ld, res );
955 msg = ldap_next_message( ld, msg ) )
957 if ( nresponses++ ) putchar('\n');
959 if ( nresponses_psearch >= 0 )
960 nresponses_psearch++;
963 switch( ldap_msgtype( msg ) ) {
964 case LDAP_RES_SEARCH_ENTRY:
966 print_entry( ld, msg, attrsonly );
969 case LDAP_RES_SEARCH_REFERENCE:
971 print_reference( ld, msg );
974 case LDAP_RES_EXTENDED:
976 print_extended( ld, msg );
978 if( ldap_msgid( msg ) == 0 ) {
979 /* unsolicited extended operation */
984 if ( cancel_msgid != -1 &&
985 cancel_msgid == ldap_msgid( msg ) ) {
986 printf("Cancelled \n");
987 printf("cancel_msgid = %d\n", cancel_msgid);
994 case LDAP_RES_EXTENDED_PARTIAL:
996 print_partial( ld, msg );
999 case LDAP_RES_SEARCH_RESULT:
1000 rc = print_result( ld, msg, 1 );
1001 if ( pageSize != 0 ) {
1002 rc = parse_page_control( ld, msg, &cookie );
1005 #ifdef LDAP_CLIENT_UPDATE
1006 if ( lcup == LDAP_CUP_PERSIST_ONLY ||
1007 lcup == LDAP_CUP_SYNC_AND_PERSIST ) {
1011 #if defined(LDAP_CLIENT_UPDATE) && defined(LDAP_SYNC)
1015 if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1023 case LDAP_RES_INTERMEDIATE_RESP:
1024 ldap_parse_intermediate_resp_result( ld, msg, &retoid, &retdata, 0 );
1025 nresponses_psearch = 0;
1027 if ( strcmp( retoid, LDAP_SYNC_INFO ) ) {
1028 printf("Unexpected Intermediate Response\n");
1029 ldap_memfree( retoid );
1030 ber_bvfree( retdata );
1033 printf("SyncInfo Received\n");
1034 ldap_memfree( retoid );
1035 ber_bvfree( retdata );
1041 #ifdef LDAP_CLIENT_UPDATE
1042 if ( lcup && lcup_slimit != -1 && nresponses >= lcup_slimit ) {
1043 ldap_abandon (ld, ldap_msgid(msg));
1048 if ( ldapsync && sync_slimit != -1 &&
1049 nresponses_psearch >= sync_slimit ) {
1050 BerElement *msgidber = NULL;
1051 struct berval *msgidvalp = NULL;
1052 msgidber = ber_alloc_t(LBER_USE_DER);
1053 ber_printf(msgidber, "{i}", msgid);
1054 ber_flatten(msgidber, &msgidvalp);
1055 ldap_extended_operation(ld, LDAP_EXOP_X_CANCEL,
1056 msgidvalp, NULL, NULL, &cancel_msgid);
1057 nresponses_psearch = -1;
1063 ldap_msgfree( res );
1067 ldap_perror( ld, "ldap_result" );
1072 if ( pageSize != 0 ) {
1073 npagedresponses = npagedresponses + nresponses;
1074 npagedentries = npagedentries + nentries;
1075 npagedreferences = npagedreferences + nreferences;
1076 npagedextended = npagedextended + nextended;
1077 npagedpartial = npagedpartial + npartial;
1078 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
1079 printf( "\n# numResponses: %d\n", npagedresponses );
1080 if( nentries ) printf( "# numEntries: %d\n", npagedentries );
1081 if( nextended ) printf( "# numExtended: %d\n", npagedextended );
1082 if( npartial ) printf( "# numPartial: %d\n", npagedpartial );
1083 if( nreferences ) printf( "# numReferences: %d\n", npagedreferences );
1085 } else if ( ldif < 2 ) {
1086 printf( "\n# numResponses: %d\n", nresponses );
1087 if( nentries ) printf( "# numEntries: %d\n", nentries );
1088 if( nextended ) printf( "# numExtended: %d\n", nextended );
1089 if( npartial ) printf( "# numPartial: %d\n", npartial );
1090 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1097 /* This is the original version, the old way of doing things. */
1105 char tmpfname[ 256 ];
1108 BerElement *ber = NULL;
1109 struct berval **bvals;
1110 LDAPControl **ctrls = NULL;
1113 dn = ldap_get_dn( ld, entry );
1117 ufn = ldap_dn2ufn( dn );
1118 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1120 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1122 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1124 if( rc != LDAP_SUCCESS ) {
1125 fprintf(stderr, "print_entry: %d\n", rc );
1126 ldap_perror( ld, "ldap_get_entry_controls" );
1127 exit( EXIT_FAILURE );
1131 print_ctrls( ctrls );
1132 ldap_controls_free( ctrls );
1137 ufn = ldap_dn2ufn( dn );
1139 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1142 if( ufn != NULL ) ldap_memfree( ufn );
1145 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1146 a = ldap_next_attribute( ld, entry, ber ) )
1149 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1151 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1152 for ( i = 0; bvals[i] != NULL; i++ ) {
1153 if ( vals2tmp > 1 || ( vals2tmp
1154 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1157 /* write value to file */
1158 snprintf( tmpfname, sizeof tmpfname,
1159 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1163 tmpfd = mkstemp( tmpfname );
1170 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1175 if ( fwrite( bvals[ i ]->bv_val,
1176 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1185 snprintf( url, sizeof url, "%s%s", urlpre,
1186 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1189 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1192 write_ldif( LDIF_PUT_VALUE, a,
1193 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1196 ber_bvecfree( bvals );
1206 /* This is the proposed new way of doing things.
1207 * It is more efficient, but the API is non-standard.
1216 char tmpfname[ 256 ];
1219 BerElement *ber = NULL;
1220 struct berval bv, *bvals, **bvp = &bvals;
1221 LDAPControl **ctrls = NULL;
1224 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1227 ufn = ldap_dn2ufn( bv.bv_val );
1228 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1230 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1232 rc = ldap_int_get_controls( ber, &ctrls );
1234 if( rc != LDAP_SUCCESS ) {
1235 fprintf(stderr, "print_entry: %d\n", rc );
1236 ldap_perror( ld, "ldap_get_entry_controls" );
1237 exit( EXIT_FAILURE );
1241 print_ctrls( ctrls );
1242 ldap_controls_free( ctrls );
1247 ufn = ldap_dn2ufn( bv.bv_val );
1249 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1252 if( ufn != NULL ) ldap_memfree( ufn );
1254 if ( attrsonly ) bvp = NULL;
1256 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1258 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1260 if (bv.bv_val == NULL) break;
1263 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1266 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1267 if ( vals2tmp > 1 || ( vals2tmp
1268 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1271 /* write value to file */
1272 snprintf( tmpfname, sizeof tmpfname,
1273 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1274 tmpdir, bv.bv_val );
1277 tmpfd = mkstemp( tmpfname );
1284 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1289 if ( fwrite( bvals[ i ].bv_val,
1290 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1299 snprintf( url, sizeof url, "%s%s", urlpre,
1300 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1303 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1306 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1307 bvals[ i ].bv_val, bvals[ i ].bv_len );
1310 ber_memfree( bvals );
1320 static void print_reference(
1322 LDAPMessage *reference )
1326 LDAPControl **ctrls;
1329 printf("# search reference\n");
1332 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1334 if( rc != LDAP_SUCCESS ) {
1335 ldap_perror(ld, "ldap_parse_reference");
1336 exit( EXIT_FAILURE );
1341 for( i=0; refs[i] != NULL; i++ ) {
1342 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1343 "ref", refs[i], strlen(refs[i]) );
1345 ber_memvfree( (void **) refs );
1349 print_ctrls( ctrls );
1350 ldap_controls_free( ctrls );
1354 static void print_extended(
1356 LDAPMessage *extended )
1359 char *retoid = NULL;
1360 struct berval *retdata = NULL;
1363 printf("# extended result response\n");
1366 rc = ldap_parse_extended_result( ld, extended,
1367 &retoid, &retdata, 0 );
1369 if( rc != LDAP_SUCCESS ) {
1370 ldap_perror(ld, "ldap_parse_extended_result");
1371 exit( EXIT_FAILURE );
1375 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1376 "extended", retoid, retoid ? strlen(retoid) : 0 );
1378 ber_memfree( retoid );
1382 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1383 "data", retdata->bv_val, retdata->bv_len );
1385 ber_bvfree( retdata );
1388 print_result( ld, extended, 0 );
1391 static void print_partial(
1393 LDAPMessage *partial )
1396 char *retoid = NULL;
1397 struct berval *retdata = NULL;
1398 LDAPControl **ctrls = NULL;
1401 printf("# extended partial response\n");
1404 rc = ldap_parse_extended_partial( ld, partial,
1405 &retoid, &retdata, &ctrls, 0 );
1407 if( rc != LDAP_SUCCESS ) {
1408 ldap_perror(ld, "ldap_parse_extended_partial");
1409 exit( EXIT_FAILURE );
1413 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1414 "partial", retoid, retoid ? strlen(retoid) : 0 );
1417 ber_memfree( retoid );
1421 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1422 "data", retdata->bv_val, retdata->bv_len );
1425 ber_bvfree( retdata );
1429 print_ctrls( ctrls );
1430 ldap_controls_free( ctrls );
1434 static int print_result(
1436 LDAPMessage *result, int search )
1440 char *matcheddn = NULL;
1443 LDAPControl **ctrls = NULL;
1447 printf("# search result\n");
1450 printf("%s: %d\n", "search", ldap_msgid(result) );
1454 rc = ldap_parse_result( ld, result,
1455 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1457 if( rc != LDAP_SUCCESS ) {
1458 ldap_perror(ld, "ldap_parse_result");
1459 exit( EXIT_FAILURE );
1464 printf( "result: %d %s\n", err, ldap_err2string(err) );
1466 } else if ( err != LDAP_SUCCESS ) {
1467 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1470 if( matcheddn && *matcheddn ) {
1472 write_ldif( LDIF_PUT_VALUE,
1473 "matchedDN", matcheddn, strlen(matcheddn) );
1475 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1478 ber_memfree( matcheddn );
1481 if( text && *text ) {
1483 write_ldif( LDIF_PUT_TEXT, "text",
1484 text, strlen(text) );
1486 fprintf( stderr, "Additional information: %s\n", text );
1489 ber_memfree( text );
1494 for( i=0; refs[i] != NULL; i++ ) {
1496 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1498 fprintf( stderr, "Referral: %s\n", refs[i] );
1502 ber_memvfree( (void **) refs );
1506 print_ctrls( ctrls );
1507 ldap_controls_free( ctrls );
1513 static void print_ctrls(
1514 LDAPControl **ctrls )
1517 for(i=0; ctrls[i] != NULL; i++ ) {
1518 /* control: OID criticality base64value */
1519 struct berval *b64 = NULL;
1524 len += strlen( ctrls[i]->ldctl_oid );
1526 /* add enough for space after OID and the critical value itself */
1527 len += ctrls[i]->ldctl_iscritical
1528 ? sizeof("true") : sizeof("false");
1530 /* convert to base64 */
1531 if( ctrls[i]->ldctl_value.bv_len ) {
1532 b64 = ber_memalloc( sizeof(struct berval) );
1534 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1535 ctrls[i]->ldctl_value.bv_len ) + 1;
1536 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1538 b64->bv_len = lutil_b64_ntop(
1539 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1540 b64->bv_val, b64->bv_len );
1544 len += 1 + b64->bv_len;
1547 str = malloc( len + 1 );
1549 strcpy( str, ": " );
1553 strcat( str, ctrls[i]->ldctl_oid );
1554 strcat( str, ctrls[i]->ldctl_iscritical
1555 ? " true" : " false" );
1559 strcat(str, b64->bv_val );
1563 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1564 "control", str, len );
1573 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1577 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1581 fputs( ldif, stdout );
1582 ber_memfree( ldif );
1591 LDAPMessage *result,
1592 struct berval *cookie )
1596 LDAPControl **ctrl = NULL;
1597 LDAPControl *ctrlp = NULL;
1600 struct berval servercookie = { 0, NULL };
1603 rc = ldap_parse_result( ld, result,
1604 &err, NULL, NULL, NULL, &ctrl, 0 );
1606 if( rc != LDAP_SUCCESS ) {
1607 ldap_perror(ld, "ldap_parse_result");
1608 exit( EXIT_FAILURE );
1611 if ( err != LDAP_SUCCESS ) {
1612 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1616 /* Parse the control value
1617 * searchResult ::= SEQUENCE {
1618 * size INTEGER (0..maxInt),
1619 * -- result set size estimate from server - unused
1620 * cookie OCTET STRING
1623 ber = ber_init( &ctrlp->ldctl_value );
1624 if ( ber == NULL ) {
1625 fprintf( stderr, "Internal error.\n" );
1626 return EXIT_FAILURE;
1629 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1630 ber_dupbv( cookie, &servercookie );
1631 (void) ber_free( ber, 1 );
1633 if( tag == LBER_ERROR ) {
1634 fprintf( stderr, "Paged results response control could not be decoded.\n" );
1635 return EXIT_FAILURE;
1638 if( entriesLeft < 0 ) {
1639 fprintf( stderr, "Invalid entries estimate in paged results response.\n" );
1640 return EXIT_FAILURE;
1644 ldap_controls_free( ctrl );
1646 morePagedResults = 0;