1 /* ldapsearch -- a tool for searching LDAP directories */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 1998-2001 Net Boolean Incorporated.
8 * Portions Copyright 2001-2003 IBM Corporation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that this notice is preserved and that due credit is given
24 * to the University of Michigan at Ann Arbor. The name of the
25 * University may not be used to endorse or promote products derived
26 * from this software without specific prior written permission. This
27 * software is provided ``as is'' without express or implied warranty.
30 * This work was originally developed by the University of Michigan
31 * (as part of U-MICH LDAP). Additional significant contributors
43 #include <ac/stdlib.h>
46 #include <ac/string.h>
47 #include <ac/unistd.h>
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
65 #include "lutil_ldap.h"
66 #include "ldap_defaults.h"
73 static int scope = LDAP_SCOPE_SUBTREE;
74 static int deref = -1;
76 static int timelimit = -1;
77 static int sizelimit = -1;
79 static char *def_tmpdir;
80 static char *def_urlpre;
86 fprintf( stderr, _("usage: %s [options] [filter [attributes...]]\nwhere:\n"), prog);
87 fprintf( stderr, _(" filter\tRFC-2254 compliant LDAP search filter\n"));
88 fprintf( stderr, _(" attributes\twhitespace-separated list of attribute descriptions\n"));
89 fprintf( stderr, _(" which may include:\n"));
90 fprintf( stderr, _(" 1.1 no attributes\n"));
91 fprintf( stderr, _(" * all user attributes\n"));
92 fprintf( stderr, _(" + all operational attributes\n"));
95 fprintf( stderr, _("Search options:\n"));
96 fprintf( stderr, _(" -a deref one of never (default), always, search, or find\n"));
97 fprintf( stderr, _(" -A retrieve attribute names only (no values)\n"));
98 fprintf( stderr, _(" -b basedn base dn for search\n"));
99 fprintf( stderr, _(" -E [!]<ctrl>[=<ctrlparam>] search controls (! indicates criticality)\n"));
100 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
101 fprintf( stderr, _(" [!]domainScope (domain scope)\n"));
103 fprintf( stderr, _(" [!]mv=<filter> (matched values filter)\n"));
104 #ifdef LDAP_CONTROL_PAGEDRESULTS
105 fprintf( stderr, _(" [!]pr=<size> (paged results)\n"));
107 #ifdef LDAP_CONTROL_SUBENTRIES
108 fprintf( stderr, _(" [!]subentries[=true|false] (subentries)\n"));
110 fprintf( stderr, _(" [!]sync=ro[/<cookie>] (LDAP Sync refreshOnly)\n"));
111 fprintf( stderr, _(" rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
112 fprintf( stderr, _(" -F prefix URL prefix for files (default: %s)\n"), def_urlpre);
113 fprintf( stderr, _(" -l limit time limit (in seconds) for search\n"));
114 fprintf( stderr, _(" -L print responses in LDIFv1 format\n"));
115 fprintf( stderr, _(" -LL print responses in LDIF format without comments\n"));
116 fprintf( stderr, _(" -LLL print responses in LDIF format without comments\n"));
117 fprintf( stderr, _(" and version\n"));
118 fprintf( stderr, _(" -s scope one of base, one, or sub (search scope)\n"));
119 fprintf( stderr, _(" -S attr sort the results by attribute `attr'\n"));
120 fprintf( stderr, _(" -t write binary values to files in temporary directory\n"));
121 fprintf( stderr, _(" -tt write all values to files in temporary directory\n"));
122 fprintf( stderr, _(" -T path write files to directory specified by path (default: %s)\n"), def_tmpdir);
123 fprintf( stderr, _(" -u include User Friendly entry names in the output\n"));
124 fprintf( stderr, _(" -z limit size limit (in entries) for search\n"));
126 exit( EXIT_FAILURE );
129 static void print_entry LDAP_P((
134 static void print_reference(
136 LDAPMessage *reference );
138 static void print_extended(
140 LDAPMessage *extended );
142 static void print_partial(
144 LDAPMessage *partial );
146 static int print_result(
151 static void print_ctrls(
152 LDAPControl **ctrls );
154 static int write_ldif LDAP_P((
160 static int dosearch LDAP_P((
168 LDAPControl **sctrls,
169 LDAPControl **cctrls,
170 struct timeval *timeout,
173 static char *tmpdir = NULL;
174 static char *urlpre = NULL;
175 static char *base = NULL;
176 static char *sortattr = NULL;
177 static int includeufn, vals2tmp = 0, ldif = 0;
179 static int subentries = 0, valuesReturnFilter = 0;
180 static char *vrFilter = NULL;
182 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
183 static int domainScope = 0;
186 static int ldapsync = 0;
187 static struct berval sync_cookie = { 0, NULL };
188 static int sync_slimit = -1;
190 #ifdef LDAP_CONTROL_PAGEDRESULTS
191 static int pagedResults = 0;
192 static ber_int_t pageSize = 0;
193 static ber_int_t entriesLeft = 0;
194 static ber_int_t morePagedResults = 1;
195 static struct berval cookie = { 0, NULL };
196 static int npagedresponses;
197 static int npagedentries;
198 static int npagedreferences;
199 static int npagedextended;
200 static int npagedpartial;
202 static int parse_page_control(
205 struct berval *cookie );
213 if (*LDAP_DIRSEP != '/') {
214 for (p = url; *p; p++) {
215 if (*p == *LDAP_DIRSEP)
222 const char options[] = "a:Ab:E:F:l:Ls:S:tT:uz:"
223 "Cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
226 handle_private_option( int i )
229 char *control, *cvalue;
231 case 'a': /* set alias deref option */
232 if ( strcasecmp( optarg, "never" ) == 0 ) {
233 deref = LDAP_DEREF_NEVER;
234 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
235 deref = LDAP_DEREF_SEARCHING;
236 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
237 deref = LDAP_DEREF_FINDING;
238 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
239 deref = LDAP_DEREF_ALWAYS;
241 fprintf( stderr, _("alias deref should be never, search, find, or always\n") );
245 case 'A': /* retrieve attribute names only -- no values */
248 case 'b': /* search base */
249 base = strdup( optarg );
251 case 'E': /* search controls */
252 if( protocol == LDAP_VERSION2 ) {
253 fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
255 exit( EXIT_FAILURE );
258 /* should be extended to support comma separated list of
259 * [!]key[=value] parameters, e.g. -E !foo,bar=567
264 if( optarg[0] == '!' ) {
269 control = strdup( optarg );
270 if ( (cvalue = strchr( control, '=' )) != NULL ) {
274 if ( strcasecmp( control, "mv" ) == 0 ) {
275 /* ValuesReturnFilter control */
276 if( valuesReturnFilter ) {
277 fprintf( stderr, _("ValuesReturnFilter previously specified\n"));
278 exit( EXIT_FAILURE );
280 valuesReturnFilter= 1 + crit;
282 if ( cvalue == NULL ) {
284 _("missing filter in ValuesReturnFilter control\n"));
285 exit( EXIT_FAILURE );
289 protocol = LDAP_VERSION3;
291 #ifdef LDAP_CONTROL_PAGEDRESULTS
292 } else if ( strcasecmp( control, "pr" ) == 0 ) {
294 /* PagedResults control */
295 if ( pagedResults != 0 ) {
296 fprintf( stderr, _("PagedResultsControl previously specified\n") );
297 exit( EXIT_FAILURE );
300 num = sscanf( cvalue, "%d", &tmp );
302 fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), cvalue);
303 exit( EXIT_FAILURE );
306 pageSize = (ber_int_t) tmp;
307 pagedResults = 1 + crit;
310 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
311 } else if ( strcasecmp( control, "domainScope" ) == 0 ) {
314 _("domainScope control previously specified\n"));
315 exit( EXIT_FAILURE );
317 if( cvalue != NULL ) {
319 _("domainScope: no control value expected\n") );
323 domainScope = 1 + crit;
325 #ifdef LDAP_CONTROL_SUBENTRIES
326 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
328 fprintf( stderr, _("subentries control previously specified\n"));
329 exit( EXIT_FAILURE );
331 if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
333 } else if ( strcasecmp( cvalue, "false") == 0 ) {
337 _("subentries control value \"%s\" invalid\n"),
339 exit( EXIT_FAILURE );
341 if( crit ) subentries *= -1;
344 } else if ( strcasecmp( control, "sync" ) == 0 ) {
348 fprintf( stderr, _("ldap sync control previously specified\n") );
349 exit( EXIT_FAILURE );
351 if ( cvalue == NULL ) {
353 _("missing specification of ldap sync control\n"));
354 exit( EXIT_FAILURE );
356 if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
357 ldapsync = LDAP_SYNC_REFRESH_ONLY;
358 cookiep = strchr( cvalue, '/' );
359 if ( cookiep != NULL ) {
361 if ( *cookiep != '\0' ) {
362 ber_str2bv( cookiep, 0, 0, &sync_cookie );
365 } else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
366 ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
367 cookiep = strchr( cvalue, '/' );
368 if ( cookiep != NULL ) {
372 slimitp = strchr( cvalue, '/' );
373 if ( slimitp != NULL ) {
376 if ( cookiep != NULL && *cookiep != '\0' )
377 ber_str2bv( cookiep, 0, 0, &sync_cookie );
378 if ( slimitp != NULL && *slimitp != '\0' )
379 sync_slimit = atoi( slimitp );
382 _("ldap sync control value \"%s\" invalid\n"),
384 exit( EXIT_FAILURE );
386 if ( crit ) ldapsync *= -1;
389 fprintf( stderr, _("Invalid control name: %s\n"), control );
393 case 'F': /* uri prefix */
394 if( urlpre ) free( urlpre );
395 urlpre = strdup( optarg );
397 case 'l': /* time limit */
398 timelimit = atoi( optarg );
399 if( timelimit < 0 ) {
400 fprintf( stderr, _("%s: invalid timelimit (%d) specified\n"),
402 exit( EXIT_FAILURE );
405 case 'L': /* print entries in LDIF format */
408 case 's': /* search scope */
409 if ( strcasecmp( optarg, "base" ) == 0 ) {
410 scope = LDAP_SCOPE_BASE;
411 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
412 scope = LDAP_SCOPE_ONELEVEL;
413 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
414 scope = LDAP_SCOPE_SUBTREE;
416 fprintf( stderr, _("scope should be base, one, or sub\n") );
420 case 'S': /* sort attribute */
421 sortattr = strdup( optarg );
423 case 't': /* write attribute values to TMPDIR files */
426 case 'T': /* tmpdir */
427 if( tmpdir ) free( tmpdir );
428 tmpdir = strdup( optarg );
430 case 'u': /* include UFN */
433 case 'z': /* size limit */
434 sizelimit = atoi( optarg );
444 private_conn_setup( LDAP *ld )
447 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
449 fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
450 exit( EXIT_FAILURE );
452 if (timelimit != -1 &&
453 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
455 fprintf( stderr, _("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
456 exit( EXIT_FAILURE );
458 if (sizelimit != -1 &&
459 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
461 fprintf( stderr, _("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
462 exit( EXIT_FAILURE );
468 main( int argc, char **argv )
470 char *filtpattern, **attrs = NULL, line[BUFSIZ];
474 BerElement *seber = NULL, *vrber = NULL, *prber = NULL;
476 BerElement *syncber = NULL;
477 struct berval *syncbvalp = NULL;
481 #ifdef LDAP_CONTROL_PAGEDRESULTS
482 npagedresponses = npagedentries = npagedreferences =
483 npagedextended = npagedpartial = 0;
486 prog = lutil_progname( "ldapsearch", argc, argv );
488 lutil_log_initialize(argc, argv);
490 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
491 (def_tmpdir = getenv("TMP")) == NULL &&
492 (def_tmpdir = getenv("TEMP")) == NULL )
494 def_tmpdir = LDAP_TMPDIR;
498 def_tmpdir = LDAP_TMPDIR;
500 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
502 if( def_urlpre == NULL ) {
507 sprintf( def_urlpre, "file:///%s/",
508 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
510 urlize( def_urlpre );
512 tool_args( argc, argv );
514 if (( argc - optind < 1 ) ||
515 ( *argv[optind] != '(' /*')'*/ &&
516 ( strchr( argv[optind], '=' ) == NULL ) ) )
518 filtpattern = "(objectclass=*)";
520 filtpattern = strdup( argv[optind++] );
523 if ( argv[optind] != NULL ) {
524 attrs = &argv[optind];
527 if ( infile != NULL ) {
528 if ( infile[0] == '-' && infile[1] == '\0' ) {
530 } else if (( fp = fopen( infile, "r" )) == NULL ) {
536 if ( tmpdir == NULL ) {
539 if ( urlpre == NULL )
543 if( urlpre == NULL ) {
544 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
546 if( urlpre == NULL ) {
551 sprintf( urlpre, "file:///%s/",
552 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
560 ld = tool_conn_setup( 0, &private_conn_setup );
562 if ( pw_file || want_bindpw ) {
564 rc = lutil_get_filed_password( pw_file, &passwd );
565 if( rc ) return EXIT_FAILURE;
567 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
568 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
575 if ( assertion || authzid || manageDSAit || noop
576 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
579 #ifdef LDAP_CONTROL_PAGEDRESULTS
583 || subentries || valuesReturnFilter )
589 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
591 c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
592 c[i].ldctl_value.bv_val = NULL;
593 c[i].ldctl_value.bv_len = 0;
594 c[i].ldctl_iscritical = domainScope > 1;
599 #ifdef LDAP_CONTROL_SUBENTRIES
601 if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
605 err = ber_printf( seber, "{b}", abs(subentries) == 1 ? 0 : 1 );
607 ber_free( seber, 1 );
608 fprintf( stderr, _("Subentries control encoding error!\n") );
612 if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
616 c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
617 c[i].ldctl_iscritical = subentries < 1;
623 if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
627 if ( sync_cookie.bv_len == 0 ) {
628 err = ber_printf( syncber, "{e}", abs(ldapsync) );
630 err = ber_printf( syncber, "{eO}", abs(ldapsync),
634 if ( err == LBER_ERROR ) {
635 ber_free( syncber, 1 );
636 fprintf( stderr, _("ldap sync control encoding error!\n") );
640 if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
644 c[i].ldctl_oid = LDAP_CONTROL_SYNC;
645 c[i].ldctl_value = (*syncbvalp);
646 c[i].ldctl_iscritical = ldapsync < 0;
650 if ( valuesReturnFilter ) {
651 if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
655 if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
656 ber_free( vrber, 1 );
657 fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
661 if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
665 c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
666 c[i].ldctl_iscritical = valuesReturnFilter > 1;
670 #ifdef LDAP_CONTROL_PAGEDRESULTS
671 if ( pagedResults ) {
672 if (( prber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
676 ber_printf( prber, "{iO}", pageSize, &cookie );
677 if ( ber_flatten2( prber, &c[i].ldctl_value, 0 ) == -1 ) {
681 c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
682 c[i].ldctl_iscritical = pagedResults > 1;
687 tool_server_controls( ld, c, i );
689 ber_free( seber, 1 );
690 ber_free( vrber, 1 );
691 ber_free( prber, 1 );
695 fprintf( stderr, _("filter%s: %s\nrequesting: "),
696 infile != NULL ? _(" pattern") : "",
699 if ( attrs == NULL ) {
700 fprintf( stderr, _("ALL") );
702 for ( i = 0; attrs[ i ] != NULL; ++i ) {
703 fprintf( stderr, "%s ", attrs[ i ] );
706 fprintf( stderr, "\n" );
710 printf( _("# extended LDIF\n") );
711 } else if ( ldif < 3 ) {
712 printf( _("version: %d\n\n"), 1 );
717 printf(_("# LDAPv%d\n"), protocol);
718 printf(_("# base <%s> with scope %s\n"),
719 base ? base : "", (scope == LDAP_SCOPE_BASE) ? "base"
720 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"));
721 printf(_("# filter%s: %s\n"), infile != NULL ? _(" pattern") : "",
723 printf(_("# requesting: "));
725 if ( attrs == NULL ) {
728 for ( i = 0; attrs[ i ] != NULL; ++i ) {
729 printf( "%s ", attrs[ i ] );
734 printf(_("\n# with manageDSAit %scontrol"),
735 manageDSAit > 1 ? _("critical ") : "" );
738 printf(_("\n# with noop %scontrol"),
739 noop > 1 ? _("critical ") : "" );
742 printf(_("\n# with subentries %scontrol: %s"),
743 subentries < 0 ? _("critical ") : "",
744 abs(subentries) == 1 ? "false" : "true" );
746 if ( valuesReturnFilter ) {
747 printf(_("\n# with valuesReturnFilter %scontrol: %s"),
748 valuesReturnFilter > 1 ? _("critical ") : "", vrFilter );
750 #ifdef LDAP_CONTROL_PAGEDRESULTS
752 printf(_("\n# with pagedResults %scontrol: size=%d"),
753 (pagedResults > 1) ? _("critical ") : "",
758 printf( _("\n#\n\n") );
761 if ( infile == NULL ) {
762 rc = dosearch( ld, base, scope, NULL, filtpattern,
763 attrs, attrsonly, NULL, NULL, NULL, -1 );
768 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
769 line[ strlen( line ) - 1 ] = '\0';
775 rc = dosearch( ld, base, scope, filtpattern, line,
776 attrs, attrsonly, NULL, NULL, NULL, -1 );
783 #ifdef LDAP_CONTROL_PAGEDRESULTS
784 if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) {
786 int i, moreEntries, tmpSize;
788 /* Loop to get the next pages when
789 * enter is pressed on the terminal.
791 if ( entriesLeft > 0 ) {
792 printf( _("Estimate entries: %d\n"), entriesLeft );
794 printf( _("Press [size] Enter for the next {%d|size} entries.\n"),
797 moreEntries = getchar();
798 while ( moreEntries != EOF && moreEntries != '\n' ) {
799 if ( i < (int)sizeof(buf) - 1 ) {
800 buf[i] = moreEntries;
803 moreEntries = getchar();
807 if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
808 int num = sscanf( buf, "%d", &tmpSize );
810 fprintf( stderr, _("Invalid value for PagedResultsControl, %s.\n"), buf);
814 pageSize = (ber_int_t)tmpSize;
822 #ifdef HAVE_CYRUS_SASL
826 ldap_pvt_tls_destroy();
840 LDAPControl **sctrls,
841 LDAPControl **cctrls,
842 struct timeval *timeout,
852 LDAPMessage *res, *msg;
855 struct berval *retdata = NULL;
856 int nresponses_psearch = -1;
857 int cancel_msgid = -1;
859 if( filtpatt != NULL ) {
860 filter = malloc( strlen( filtpatt ) + strlen( value ) );
861 if( filter == NULL ) {
866 sprintf( filter, filtpatt, value );
869 fprintf( stderr, _("filter: %s\n"), filter );
873 printf( _("#\n# filter: %s\n#\n"), filter );
884 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
885 sctrls, cctrls, timeout, sizelimit, &msgid );
887 if ( filtpatt != NULL ) {
891 if( rc != LDAP_SUCCESS ) {
892 fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
893 prog, ldap_err2string( rc ), rc );
897 nresponses = nentries = nreferences = nextended = npartial = 0;
901 while ((rc = ldap_result( ld, LDAP_RES_ANY,
902 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
906 (void) ldap_sort_entries( ld, &res,
907 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
910 for ( msg = ldap_first_message( ld, res );
912 msg = ldap_next_message( ld, msg ) )
914 if ( nresponses++ ) putchar('\n');
915 if ( nresponses_psearch >= 0 )
916 nresponses_psearch++;
918 switch( ldap_msgtype( msg ) ) {
919 case LDAP_RES_SEARCH_ENTRY:
921 print_entry( ld, msg, attrsonly );
924 case LDAP_RES_SEARCH_REFERENCE:
926 print_reference( ld, msg );
929 case LDAP_RES_EXTENDED:
931 print_extended( ld, msg );
933 if( ldap_msgid( msg ) == 0 ) {
934 /* unsolicited extended operation */
938 if ( cancel_msgid != -1 &&
939 cancel_msgid == ldap_msgid( msg ) ) {
940 printf(_("Cancelled \n"));
941 printf(_("cancel_msgid = %d\n"), cancel_msgid);
946 case LDAP_RES_SEARCH_RESULT:
947 rc = print_result( ld, msg, 1 );
948 #ifdef LDAP_CONTROL_PAGEDRESULTS
949 if ( pageSize != 0 ) {
950 rc = parse_page_control( ld, msg, &cookie );
954 if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
960 case LDAP_RES_INTERMEDIATE:
962 ldap_parse_intermediate( ld, msg,
963 &retoid, &retdata, NULL, 0 );
965 nresponses_psearch = 0;
967 if ( strcmp( retoid, LDAP_SYNC_INFO ) == 0 ) {
968 printf(_("SyncInfo Received\n"));
969 ldap_memfree( retoid );
970 ber_bvfree( retdata );
974 print_partial( ld, msg );
975 ldap_memfree( retoid );
976 ber_bvfree( retdata );
980 if ( ldapsync && sync_slimit != -1 &&
981 nresponses_psearch >= sync_slimit ) {
982 BerElement *msgidber = NULL;
983 struct berval *msgidvalp = NULL;
984 msgidber = ber_alloc_t(LBER_USE_DER);
985 ber_printf(msgidber, "{i}", msgid);
986 ber_flatten(msgidber, &msgidvalp);
987 ldap_extended_operation(ld, LDAP_EXOP_X_CANCEL,
988 msgidvalp, NULL, NULL, &cancel_msgid);
989 nresponses_psearch = -1;
997 ldap_perror( ld, "ldap_result" );
1002 ldap_msgfree( res );
1003 #ifdef LDAP_CONTROL_PAGEDRESULTS
1004 if ( pageSize != 0 ) {
1005 npagedresponses = npagedresponses + nresponses;
1006 npagedentries = npagedentries + nentries;
1007 npagedreferences = npagedreferences + nreferences;
1008 npagedextended = npagedextended + nextended;
1009 npagedpartial = npagedpartial + npartial;
1010 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
1011 printf( _("\n# numResponses: %d\n"), npagedresponses );
1012 if( nentries ) printf( _("# numEntries: %d\n"), npagedentries );
1013 if( nextended ) printf( _("# numExtended: %d\n"), npagedextended );
1014 if( npartial ) printf( _("# numPartial: %d\n"), npagedpartial );
1015 if( nreferences ) printf( _("# numReferences: %d\n"), npagedreferences );
1020 printf( _("\n# numResponses: %d\n"), nresponses );
1021 if( nentries ) printf( _("# numEntries: %d\n"), nentries );
1022 if( nextended ) printf( _("# numExtended: %d\n"), nextended );
1023 if( npartial ) printf( _("# numPartial: %d\n"), npartial );
1024 if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
1031 /* This is the original version, the old way of doing things. */
1039 char tmpfname[ 256 ];
1042 BerElement *ber = NULL;
1043 struct berval **bvals;
1044 LDAPControl **ctrls = NULL;
1047 dn = ldap_get_dn( ld, entry );
1051 ufn = ldap_dn2ufn( dn );
1052 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1054 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1056 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1058 if( rc != LDAP_SUCCESS ) {
1059 fprintf(stderr, _("print_entry: %d\n"), rc );
1060 ldap_perror( ld, "ldap_get_entry_controls" );
1061 exit( EXIT_FAILURE );
1065 print_ctrls( ctrls );
1066 ldap_controls_free( ctrls );
1071 ufn = ldap_dn2ufn( dn );
1073 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1076 if( ufn != NULL ) ldap_memfree( ufn );
1079 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1080 a = ldap_next_attribute( ld, entry, ber ) )
1083 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1085 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1086 for ( i = 0; bvals[i] != NULL; i++ ) {
1087 if ( vals2tmp > 1 || ( vals2tmp
1088 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1091 /* write value to file */
1092 snprintf( tmpfname, sizeof tmpfname,
1093 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1097 tmpfd = mkstemp( tmpfname );
1104 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1109 if ( fwrite( bvals[ i ]->bv_val,
1110 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1119 snprintf( url, sizeof url, "%s%s", urlpre,
1120 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1123 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1126 write_ldif( LDIF_PUT_VALUE, a,
1127 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1130 ber_bvecfree( bvals );
1140 /* This is the proposed new way of doing things.
1141 * It is more efficient, but the API is non-standard.
1150 char tmpfname[ 256 ];
1153 BerElement *ber = NULL;
1154 struct berval bv, *bvals, **bvp = &bvals;
1155 LDAPControl **ctrls = NULL;
1158 rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1161 ufn = ldap_dn2ufn( bv.bv_val );
1162 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1164 write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1166 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1167 if( rc != LDAP_SUCCESS ) {
1168 fprintf(stderr, _("print_entry: %d\n"), rc );
1169 ldap_perror( ld, "ldap_get_entry_controls" );
1170 exit( EXIT_FAILURE );
1174 print_ctrls( ctrls );
1175 ldap_controls_free( ctrls );
1180 ufn = ldap_dn2ufn( bv.bv_val );
1182 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1185 if( ufn != NULL ) ldap_memfree( ufn );
1187 if ( attrsonly ) bvp = NULL;
1189 for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1191 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1193 if (bv.bv_val == NULL) break;
1196 write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1198 } else if ( bvals ) {
1199 for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1200 if ( vals2tmp > 1 || ( vals2tmp
1201 && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1204 /* write value to file */
1205 snprintf( tmpfname, sizeof tmpfname,
1206 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1207 tmpdir, bv.bv_val );
1210 tmpfd = mkstemp( tmpfname );
1217 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1222 if ( fwrite( bvals[ i ].bv_val,
1223 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1232 snprintf( url, sizeof url, "%s%s", urlpre,
1233 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1236 write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1239 write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1240 bvals[ i ].bv_val, bvals[ i ].bv_len );
1243 ber_memfree( bvals );
1253 static void print_reference(
1255 LDAPMessage *reference )
1259 LDAPControl **ctrls;
1262 printf(_("# search reference\n"));
1265 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1267 if( rc != LDAP_SUCCESS ) {
1268 ldap_perror(ld, "ldap_parse_reference");
1269 exit( EXIT_FAILURE );
1274 for( i=0; refs[i] != NULL; i++ ) {
1275 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1276 "ref", refs[i], strlen(refs[i]) );
1278 ber_memvfree( (void **) refs );
1282 print_ctrls( ctrls );
1283 ldap_controls_free( ctrls );
1287 static void print_extended(
1289 LDAPMessage *extended )
1292 char *retoid = NULL;
1293 struct berval *retdata = NULL;
1296 printf(_("# extended result response\n"));
1299 rc = ldap_parse_extended_result( ld, extended,
1300 &retoid, &retdata, 0 );
1302 if( rc != LDAP_SUCCESS ) {
1303 ldap_perror(ld, "ldap_parse_extended_result");
1304 exit( EXIT_FAILURE );
1308 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1309 "extended", retoid, retoid ? strlen(retoid) : 0 );
1311 ber_memfree( retoid );
1315 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1316 "data", retdata->bv_val, retdata->bv_len );
1318 ber_bvfree( retdata );
1321 print_result( ld, extended, 0 );
1324 static void print_partial(
1326 LDAPMessage *partial )
1329 char *retoid = NULL;
1330 struct berval *retdata = NULL;
1331 LDAPControl **ctrls = NULL;
1334 printf(_("# extended partial response\n"));
1337 rc = ldap_parse_intermediate( ld, partial,
1338 &retoid, &retdata, &ctrls, 0 );
1340 if( rc != LDAP_SUCCESS ) {
1341 ldap_perror(ld, "ldap_parse_intermediate");
1342 exit( EXIT_FAILURE );
1346 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1347 "partial", retoid, retoid ? strlen(retoid) : 0 );
1350 ber_memfree( retoid );
1354 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1355 "data", retdata->bv_val, retdata->bv_len );
1358 ber_bvfree( retdata );
1362 print_ctrls( ctrls );
1363 ldap_controls_free( ctrls );
1367 static int print_result(
1369 LDAPMessage *result, int search )
1373 char *matcheddn = NULL;
1376 LDAPControl **ctrls = NULL;
1380 printf(_("# search result\n"));
1383 printf("%s: %d\n", _("search"), ldap_msgid(result) );
1387 rc = ldap_parse_result( ld, result,
1388 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1390 if( rc != LDAP_SUCCESS ) {
1391 ldap_perror(ld, "ldap_parse_result");
1392 exit( EXIT_FAILURE );
1397 printf( _("result: %d %s\n"), err, ldap_err2string(err) );
1399 } else if ( err != LDAP_SUCCESS ) {
1400 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1406 write_ldif( LDIF_PUT_VALUE,
1407 "matchedDN", matcheddn, strlen(matcheddn) );
1409 fprintf( stderr, _("Matched DN: %s\n"), matcheddn );
1413 ber_memfree( matcheddn );
1419 write_ldif( LDIF_PUT_TEXT, "text",
1420 text, strlen(text) );
1422 fprintf( stderr, _("Additional information: %s\n"), text );
1426 ber_memfree( text );
1431 for( i=0; refs[i] != NULL; i++ ) {
1433 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1435 fprintf( stderr, _("Referral: %s\n"), refs[i] );
1439 ber_memvfree( (void **) refs );
1443 print_ctrls( ctrls );
1444 ldap_controls_free( ctrls );
1450 static void print_ctrls(
1451 LDAPControl **ctrls )
1454 for(i=0; ctrls[i] != NULL; i++ ) {
1455 /* control: OID criticality base64value */
1456 struct berval *b64 = NULL;
1461 len += strlen( ctrls[i]->ldctl_oid );
1463 /* add enough for space after OID and the critical value itself */
1464 len += ctrls[i]->ldctl_iscritical
1465 ? sizeof("true") : sizeof("false");
1467 /* convert to base64 */
1468 if( ctrls[i]->ldctl_value.bv_len ) {
1469 b64 = ber_memalloc( sizeof(struct berval) );
1471 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1472 ctrls[i]->ldctl_value.bv_len ) + 1;
1473 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1475 b64->bv_len = lutil_b64_ntop(
1476 (unsigned char *) ctrls[i]->ldctl_value.bv_val,
1477 ctrls[i]->ldctl_value.bv_len,
1478 b64->bv_val, b64->bv_len );
1482 len += 1 + b64->bv_len;
1485 str = malloc( len + 1 );
1487 strcpy( str, ": " );
1491 strcat( str, ctrls[i]->ldctl_oid );
1492 strcat( str, ctrls[i]->ldctl_iscritical
1493 ? " true" : " false" );
1497 strcat(str, b64->bv_val );
1501 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1502 "control", str, len );
1511 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1515 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1519 fputs( ldif, stdout );
1520 ber_memfree( ldif );
1526 #ifdef LDAP_CONTROL_PAGEDRESULTS
1530 LDAPMessage *result,
1531 struct berval *cookie )
1535 LDAPControl **ctrl = NULL;
1536 LDAPControl *ctrlp = NULL;
1539 struct berval servercookie = { 0, NULL };
1541 rc = ldap_parse_result( ld, result,
1542 &err, NULL, NULL, NULL, &ctrl, 0 );
1544 if( rc != LDAP_SUCCESS ) {
1545 ldap_perror(ld, "ldap_parse_result");
1546 exit( EXIT_FAILURE );
1549 if ( err != LDAP_SUCCESS ) {
1550 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1554 /* Parse the control value
1555 * searchResult ::= SEQUENCE {
1556 * size INTEGER (0..maxInt),
1557 * -- result set size estimate from server - unused
1558 * cookie OCTET STRING
1562 ber = ber_init( &ctrlp->ldctl_value );
1563 if ( ber == NULL ) {
1564 fprintf( stderr, _("Internal error.\n") );
1565 return EXIT_FAILURE;
1568 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1569 ber_dupbv( cookie, &servercookie );
1570 (void) ber_free( ber, 1 );
1572 if( tag == LBER_ERROR ) {
1574 _("Paged results response control could not be decoded.\n") );
1575 return EXIT_FAILURE;
1578 if( entriesLeft < 0 ) {
1580 _("Invalid entries estimate in paged results response.\n") );
1581 return EXIT_FAILURE;
1584 ldap_controls_free( ctrl );
1587 morePagedResults = 0;