3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/stdlib.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
34 #include "lutil_ldap.h"
35 #include "ldap_defaults.h"
38 usage( const char *s )
41 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
42 " filter\tRFC-2254 compliant LDAP search filter\n"
43 " attributes\twhitespace-separated list of attribute descriptions\n"
44 " which may include:\n"
45 " 1.1 no attributes\n"
46 " * all user attributes\n"
47 " + all operational attributes\n"
50 " -a deref one of never (default), always, search, or find\n"
51 " -A retrieve attribute names only (no values)\n"
52 " -b basedn base dn for search\n"
53 " -F prefix URL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
54 " -l limit time limit (in seconds) for search\n"
55 " -L print responses in LDIFv1 format\n"
56 " -LL print responses in LDIF format without comments\n"
57 " -LLL print responses in LDIF format without comments\n"
59 " -s scope one of base, one, or sub (search scope)\n"
60 " -S attr sort the results by attribute `attr'\n"
61 " -t write binary values to files in temporary directory\n"
62 " -tt write all values to files in temporary directory\n"
63 " -T path write files to directory specified by path (default:\n"
65 " -u include User Friendly entry names in the output\n"
66 " -z limit size limit (in entries) for search\n"
69 " -d level set LDAP debugging level to `level'\n"
70 " -D binddn bind DN\n"
71 " -f file read operations from `file'\n"
72 " -h host LDAP server\n"
73 " -H URI LDAP Uniform Resource Indentifier(s)\n"
74 " -I use SASL Interactive mode\n"
75 " -k use Kerberos authentication\n"
76 " -K like -k, but do only step 1 of the Kerberos bind\n"
77 " -M enable Manage DSA IT control (-MM to make critical)\n"
78 " -n show what would be done but don't actually search\n"
79 " -O props SASL security properties\n"
80 " -p port port on LDAP server\n"
81 " -P version procotol version (default: 3)\n"
82 " -Q use SASL Quiet mode\n"
83 " -R realm SASL realm\n"
84 " -U authcid SASL authentication identity\n"
85 " -v run in verbose mode (diagnostics to standard output)\n"
86 " -w passwd bind passwd (for simple authentication)\n"
87 " -W prompt for bind passwd\n"
88 " -x Simple authentication\n"
89 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
90 " -Y mech SASL mechanism\n"
91 " -Z Start TLS request (-ZZ to require successful response)\n"
97 static void print_entry LDAP_P((
102 static void print_reference(
104 LDAPMessage *reference );
106 static void print_extended(
108 LDAPMessage *extended );
110 static void print_partial(
112 LDAPMessage *partial );
114 static int print_result(
119 static void print_ctrls(
120 LDAPControl **ctrls );
122 static int write_ldif LDAP_P((
128 static int dosearch LDAP_P((
136 LDAPControl **sctrls,
137 LDAPControl **cctrls,
138 struct timeval *timeout,
141 static char *tmpdir = NULL;
142 static char *urlpre = NULL;
144 static char *prog = NULL;
145 static char *binddn = NULL;
146 static struct berval passwd = { 0, NULL };
147 static char *base = NULL;
148 static char *ldaphost = NULL;
149 static char *ldapuri = NULL;
150 static int ldapport = 0;
151 #ifdef HAVE_CYRUS_SASL
152 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
153 static char *sasl_realm = NULL;
154 static char *sasl_authc_id = NULL;
155 static char *sasl_authz_id = NULL;
156 static char *sasl_mech = NULL;
157 static char *sasl_secprops = NULL;
159 static int use_tls = 0;
160 static char *sortattr = NULL;
161 static int verbose, not, includeufn, vals2tmp, ldif;
164 main( int argc, char **argv )
166 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
169 int rc, i, first, scope, deref, attrsonly, manageDSAit;
170 int referrals, timelimit, sizelimit, debug;
171 int authmethod, version, want_bindpw;
175 debug = verbose = not = vals2tmp = referrals =
176 attrsonly = manageDSAit = ldif = want_bindpw = 0;
178 lutil_log_initialize(argc, argv);
180 deref = sizelimit = timelimit = version = -1;
182 scope = LDAP_SCOPE_SUBTREE;
185 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
187 while (( i = getopt( argc, argv, "Aa:b:F:f:Ll:S:s:T:tuz:"
188 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z")) != EOF )
192 case 'a': /* set alias deref option */
193 if ( strcasecmp( optarg, "never" ) == 0 ) {
194 deref = LDAP_DEREF_NEVER;
195 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
196 deref = LDAP_DEREF_SEARCHING;
197 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
198 deref = LDAP_DEREF_FINDING;
199 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
200 deref = LDAP_DEREF_ALWAYS;
202 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
206 case 'A': /* retrieve attribute names only -- no values */
209 case 'b': /* search base */
210 base = strdup( optarg );
212 case 'f': /* input file */
213 if( infile != NULL ) {
214 fprintf( stderr, "%s: -f previously specified\n", prog );
217 infile = strdup( optarg );
219 case 'F': /* uri prefix */
220 if( urlpre ) free( urlpre );
221 urlpre = strdup( optarg );
223 case 'l': /* time limit */
224 timelimit = atoi( optarg );
225 if( timelimit < 0 ) {
226 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
231 case 'L': /* print entries in LDIF format */
234 case 's': /* search scope */
235 if ( strcasecmp( optarg, "base" ) == 0 ) {
236 scope = LDAP_SCOPE_BASE;
237 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
238 scope = LDAP_SCOPE_ONELEVEL;
239 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
240 scope = LDAP_SCOPE_SUBTREE;
242 fprintf( stderr, "scope should be base, one, or sub\n" );
246 case 'S': /* sort attribute */
247 sortattr = strdup( optarg );
249 case 'u': /* include UFN */
252 case 't': /* write attribute values to TMPDIR files */
255 case 'T': /* tmpdir */
256 if( tmpdir ) free( tmpdir );
257 tmpdir = strdup( optarg );
259 case 'z': /* size limit */
260 sizelimit = atoi( optarg );
268 debug |= atoi( optarg );
270 case 'D': /* bind DN */
271 if( binddn != NULL ) {
272 fprintf( stderr, "%s: -D previously specified\n", prog );
275 binddn = strdup( optarg );
277 case 'h': /* ldap host */
278 if( ldapuri != NULL ) {
279 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
282 if( ldaphost != NULL ) {
283 fprintf( stderr, "%s: -h previously specified\n", prog );
286 ldaphost = strdup( optarg );
288 case 'H': /* ldap URI */
289 if( ldaphost != NULL ) {
290 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
294 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
297 if( ldapuri != NULL ) {
298 fprintf( stderr, "%s: -H previously specified\n", prog );
301 ldapuri = strdup( optarg );
304 #ifdef HAVE_CYRUS_SASL
305 if( version == LDAP_VERSION2 ) {
306 fprintf( stderr, "%s: -I incompatible with version %d\n",
310 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
311 fprintf( stderr, "%s: incompatible previous "
312 "authentication choice\n",
316 authmethod = LDAP_AUTH_SASL;
317 version = LDAP_VERSION3;
318 sasl_flags = LDAP_SASL_INTERACTIVE;
321 fprintf( stderr, "%s: was not compiled with SASL support\n",
323 return( EXIT_FAILURE );
325 case 'k': /* kerberos bind */
326 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
327 if( version > LDAP_VERSION2 ) {
328 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
333 if( authmethod != -1 ) {
334 fprintf( stderr, "%s: -k incompatible with previous "
335 "authentication choice\n", prog );
339 authmethod = LDAP_AUTH_KRBV4;
341 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
345 case 'K': /* kerberos bind, part one only */
346 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
347 if( version > LDAP_VERSION2 ) {
348 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
352 if( authmethod != -1 ) {
353 fprintf( stderr, "%s: incompatible with previous "
354 "authentication choice\n", prog );
358 authmethod = LDAP_AUTH_KRBV41;
360 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
361 return( EXIT_FAILURE );
365 /* enable Manage DSA IT */
366 if( version == LDAP_VERSION2 ) {
367 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
372 version = LDAP_VERSION3;
374 case 'n': /* print deletes, don't actually do them */
378 #ifdef HAVE_CYRUS_SASL
379 if( sasl_secprops != NULL ) {
380 fprintf( stderr, "%s: -O previously specified\n", prog );
383 if( version == LDAP_VERSION2 ) {
384 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
388 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
389 fprintf( stderr, "%s: incompatible previous "
390 "authentication choice\n", prog );
393 authmethod = LDAP_AUTH_SASL;
394 version = LDAP_VERSION3;
395 sasl_secprops = strdup( optarg );
397 fprintf( stderr, "%s: not compiled with SASL support\n",
399 return( EXIT_FAILURE );
404 fprintf( stderr, "%s: -p previously specified\n", prog );
407 ldapport = atoi( optarg );
410 switch( atoi(optarg) ) {
412 if( version == LDAP_VERSION3 ) {
413 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
417 version = LDAP_VERSION2;
420 if( version == LDAP_VERSION2 ) {
421 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
425 version = LDAP_VERSION3;
428 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
431 return( EXIT_FAILURE );
434 #ifdef HAVE_CYRUS_SASL
435 if( version == LDAP_VERSION2 ) {
436 fprintf( stderr, "%s: -Q incompatible with version %d\n",
440 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
441 fprintf( stderr, "%s: incompatible previous "
442 "authentication choice\n",
446 authmethod = LDAP_AUTH_SASL;
447 version = LDAP_VERSION3;
448 sasl_flags = LDAP_SASL_QUIET;
451 fprintf( stderr, "%s: not compiled with SASL support\n",
453 return( EXIT_FAILURE );
456 #ifdef HAVE_CYRUS_SASL
457 if( sasl_realm != NULL ) {
458 fprintf( stderr, "%s: -R previously specified\n", prog );
461 if( version == LDAP_VERSION2 ) {
462 fprintf( stderr, "%s: -R incompatible with version %d\n",
466 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
467 fprintf( stderr, "%s: incompatible previous "
468 "authentication choice\n",
472 authmethod = LDAP_AUTH_SASL;
473 version = LDAP_VERSION3;
474 sasl_realm = strdup( optarg );
476 fprintf( stderr, "%s: not compiled with SASL support\n",
478 return( EXIT_FAILURE );
482 #ifdef HAVE_CYRUS_SASL
483 if( sasl_authc_id != NULL ) {
484 fprintf( stderr, "%s: -U previously specified\n", prog );
487 if( version == LDAP_VERSION2 ) {
488 fprintf( stderr, "%s: -U incompatible with version %d\n",
492 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
493 fprintf( stderr, "%s: incompatible previous "
494 "authentication choice\n",
498 authmethod = LDAP_AUTH_SASL;
499 version = LDAP_VERSION3;
500 sasl_authc_id = strdup( optarg );
502 fprintf( stderr, "%s: not compiled with SASL support\n",
504 return( EXIT_FAILURE );
507 case 'v': /* verbose mode */
510 case 'w': /* password */
511 passwd.bv_val = strdup( optarg );
515 for( p = optarg; *p != '\0'; p++ ) {
519 passwd.bv_len = strlen( passwd.bv_val );
525 #ifdef HAVE_CYRUS_SASL
526 if( sasl_mech != NULL ) {
527 fprintf( stderr, "%s: -Y previously specified\n", prog );
530 if( version == LDAP_VERSION2 ) {
531 fprintf( stderr, "%s: -Y incompatible with version %d\n",
535 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
536 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
539 authmethod = LDAP_AUTH_SASL;
540 version = LDAP_VERSION3;
541 sasl_mech = strdup( optarg );
543 fprintf( stderr, "%s: not compiled with SASL support\n",
545 return( EXIT_FAILURE );
549 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
550 fprintf( stderr, "%s: incompatible with previous "
551 "authentication choice\n", prog );
554 authmethod = LDAP_AUTH_SIMPLE;
557 #ifdef HAVE_CYRUS_SASL
558 if( sasl_authz_id != NULL ) {
559 fprintf( stderr, "%s: -X previously specified\n", prog );
562 if( version == LDAP_VERSION2 ) {
563 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
567 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
568 fprintf( stderr, "%s: -X incompatible with "
569 "authentication choice\n", prog );
572 authmethod = LDAP_AUTH_SASL;
573 version = LDAP_VERSION3;
574 sasl_authz_id = strdup( optarg );
576 fprintf( stderr, "%s: not compiled with SASL support\n",
578 return( EXIT_FAILURE );
583 if( version == LDAP_VERSION2 ) {
584 fprintf( stderr, "%s: -Z incompatible with version %d\n",
588 version = LDAP_VERSION3;
591 fprintf( stderr, "%s: not compiled with TLS support\n",
593 return( EXIT_FAILURE );
597 fprintf( stderr, "%s: unrecognized option -%c\n",
604 version = LDAP_VERSION3;
606 if (authmethod == -1 && version > LDAP_VERSION2) {
607 #ifdef HAVE_CYRUS_SASL
608 authmethod = LDAP_AUTH_SASL;
610 authmethod = LDAP_AUTH_SIMPLE;
614 if (( argc - optind < 1 ) ||
615 ( *argv[optind] != '(' /*')'*/ &&
616 ( strchr( argv[optind], '=' ) == NULL ) ) )
618 filtpattern = "(objectclass=*)";
620 filtpattern = strdup( argv[optind++] );
623 if ( (argv[optind] != NULL) && (sortattr == NULL || *sortattr == '\0') ) {
624 attrs = &argv[optind];
627 if ( infile != NULL ) {
628 if ( infile[0] == '-' && infile[1] == '\0' ) {
630 } else if (( fp = fopen( infile, "r" )) == NULL ) {
637 && (tmpdir = getenv("TMPDIR")) == NULL
638 && (tmpdir = getenv("TMP")) == NULL
639 && (tmpdir = getenv("TEMP")) == NULL )
641 tmpdir = LDAP_TMPDIR;
644 if( urlpre == NULL ) {
645 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
647 if( urlpre == NULL ) {
652 sprintf( urlpre, "file:///%s/",
653 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
655 /* urlpre should be URLized.... */
659 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
660 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
662 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
663 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
669 (void) SIGNAL( SIGPIPE, SIG_IGN );
672 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
674 fprintf( stderr, "ldap_init( %s, %d )\n",
675 ldaphost != NULL ? ldaphost : "<DEFAULT>",
679 ld = ldap_init( ldaphost, ldapport );
681 perror("ldapsearch: ldap_init");
687 fprintf( stderr, "ldap_initialize( %s )\n",
688 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
691 rc = ldap_initialize( &ld, ldapuri );
692 if( rc != LDAP_SUCCESS ) {
693 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
694 rc, ldap_err2string(rc) );
700 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
702 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
705 if (timelimit != -1 &&
706 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
708 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
711 if (sizelimit != -1 &&
712 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
714 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
719 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
720 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
722 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
723 referrals ? "on" : "off" );
727 if (version == -1 ) {
728 version = LDAP_VERSION3;
731 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
732 != LDAP_OPT_SUCCESS )
734 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
739 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
740 ldap_perror( ld, "ldap_start_tls" );
747 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
748 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
751 if ( authmethod == LDAP_AUTH_SASL ) {
752 #ifdef HAVE_CYRUS_SASL
755 if( sasl_secprops != NULL ) {
756 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
757 (void *) sasl_secprops );
759 if( rc != LDAP_OPT_SUCCESS ) {
761 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
763 return( EXIT_FAILURE );
767 defaults = lutil_sasl_defaults( ld,
774 rc = ldap_sasl_interactive_bind_s( ld, binddn,
775 sasl_mech, NULL, NULL,
776 sasl_flags, lutil_sasl_interact, defaults );
778 if( rc != LDAP_SUCCESS ) {
779 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
780 return( EXIT_FAILURE );
783 fprintf( stderr, "%s: not compiled with SASL support\n",
785 return( EXIT_FAILURE );
788 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
790 ldap_perror( ld, "ldap_bind" );
791 return( EXIT_FAILURE );
798 LDAPControl *ctrls[2];
802 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
803 c.ldctl_value.bv_val = NULL;
804 c.ldctl_value.bv_len = 0;
805 c.ldctl_iscritical = manageDSAit > 1;
807 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
809 if( err != LDAP_OPT_SUCCESS ) {
810 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
811 c.ldctl_iscritical ? "critical " : "" );
812 if( c.ldctl_iscritical ) {
813 exit( EXIT_FAILURE );
819 fprintf( stderr, "filter%s: %s\nrequesting: ",
820 infile != NULL ? " pattern" : "",
823 if ( attrs == NULL ) {
824 fprintf( stderr, "ALL" );
826 for ( i = 0; attrs[ i ] != NULL; ++i ) {
827 fprintf( stderr, "%s ", attrs[ i ] );
830 fprintf( stderr, "\n" );
834 printf( "# extended LDIF\n" );
835 } else if ( ldif < 3 ) {
836 printf( "version: %d\n\n", 1 );
840 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
842 infile != NULL ? " pattern" : "",
845 if ( attrs == NULL ) {
848 for ( i = 0; attrs[ i ] != NULL; ++i ) {
849 printf( "%s ", attrs[ i ] );
854 printf("\n# with manageDSAit %scontrol",
855 manageDSAit > 1 ? "critical " : "" );
861 if ( infile == NULL ) {
862 rc = dosearch( ld, base, scope, NULL, filtpattern,
863 attrs, attrsonly, NULL, NULL, NULL, -1 );
868 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
869 line[ strlen( line ) - 1 ] = '\0';
875 rc = dosearch( ld, base, scope, filtpattern, line,
876 attrs, attrsonly, NULL, NULL, NULL, -1 );
896 LDAPControl **sctrls,
897 LDAPControl **cctrls,
898 struct timeval *timeout,
901 char filter[ BUFSIZ ];
908 LDAPMessage *res, *msg;
911 if( filtpatt != NULL ) {
912 sprintf( filter, filtpatt, value );
915 fprintf( stderr, "filter: %s\n", filter );
919 printf( "#\n# filter: %s\n#\n", filter );
923 sprintf( filter, "%s", value );
930 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
931 sctrls, cctrls, timeout, sizelimit, &msgid );
933 if( rc != LDAP_SUCCESS ) {
934 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
935 prog, ldap_err2string( rc ), rc );
939 nresponses = nentries = nreferences = nextended = npartial = 0;
943 while ((rc = ldap_result( ld, LDAP_RES_ANY,
944 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
948 (void) ldap_sort_entries( ld, &res,
949 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
952 for ( msg = ldap_first_message( ld, res );
954 msg = ldap_next_message( ld, msg ) )
956 if( nresponses++ ) putchar('\n');
958 switch( ldap_msgtype( msg ) ) {
959 case LDAP_RES_SEARCH_ENTRY:
961 print_entry( ld, msg, attrsonly );
964 case LDAP_RES_SEARCH_REFERENCE:
966 print_reference( ld, msg );
969 case LDAP_RES_EXTENDED:
971 print_extended( ld, msg );
973 if( ldap_msgid( msg ) == 0 ) {
974 /* unsolicited extended operation */
979 case LDAP_RES_EXTENDED_PARTIAL:
981 print_partial( ld, msg );
984 case LDAP_RES_SEARCH_RESULT:
985 rc = print_result( ld, msg, 1 );
994 ldap_perror( ld, "ldap_result" );
1000 printf( "\n# numResponses: %d\n", nresponses );
1001 if( nentries ) printf( "# numEntries: %d\n", nentries );
1002 if( nextended ) printf( "# numExtended: %d\n", nextended );
1003 if( npartial ) printf( "# numPartial: %d\n", npartial );
1004 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1017 char tmpfname[ 256 ];
1020 BerElement *ber = NULL;
1021 struct berval **bvals;
1022 LDAPControl **ctrls = NULL;
1025 dn = ldap_get_dn( ld, entry );
1029 ufn = ldap_dn2ufn( dn );
1030 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1032 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1034 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1036 if( rc != LDAP_SUCCESS ) {
1037 fprintf(stderr, "print_entry: %d\n", rc );
1038 ldap_perror( ld, "ldap_get_entry_controls" );
1039 exit( EXIT_FAILURE );
1043 print_ctrls( ctrls );
1044 ldap_controls_free( ctrls );
1049 ufn = ldap_dn2ufn( dn );
1051 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1054 if( ufn != NULL ) ldap_memfree( ufn );
1057 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1058 a = ldap_next_attribute( ld, entry, ber ) )
1061 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1063 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1064 for ( i = 0; bvals[i] != NULL; i++ ) {
1065 if ( vals2tmp > 1 || ( vals2tmp
1066 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1069 /* write value to file */
1070 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1074 tmpfd = mkstemp( tmpfname );
1081 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1086 if ( fwrite( bvals[ i ]->bv_val,
1087 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1096 sprintf( url, "%s%s", urlpre,
1097 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1099 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1102 write_ldif( LDIF_PUT_VALUE, a,
1103 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1106 ber_bvecfree( bvals );
1115 static void print_reference(
1117 LDAPMessage *reference )
1121 LDAPControl **ctrls;
1124 printf("# search reference\n");
1127 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1129 if( rc != LDAP_SUCCESS ) {
1130 ldap_perror(ld, "ldap_parse_reference");
1131 exit( EXIT_FAILURE );
1136 for( i=0; refs[i] != NULL; i++ ) {
1137 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1138 "ref", refs[i], strlen(refs[i]) );
1140 ber_memvfree( (void **) refs );
1144 print_ctrls( ctrls );
1145 ldap_controls_free( ctrls );
1149 static void print_extended(
1151 LDAPMessage *extended )
1154 char *retoid = NULL;
1155 struct berval *retdata = NULL;
1158 printf("# extended result response\n");
1161 rc = ldap_parse_extended_result( ld, extended,
1162 &retoid, &retdata, 0 );
1164 if( rc != LDAP_SUCCESS ) {
1165 ldap_perror(ld, "ldap_parse_extended_result");
1166 exit( EXIT_FAILURE );
1169 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1170 "extended", retoid, retoid ? strlen(retoid) : 0 );
1171 ber_memfree( retoid );
1174 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1175 "data", retdata->bv_val, retdata->bv_len );
1176 ber_bvfree( retdata );
1179 print_result( ld, extended, 0 );
1182 static void print_partial(
1184 LDAPMessage *partial )
1187 char *retoid = NULL;
1188 struct berval *retdata = NULL;
1189 LDAPControl **ctrls = NULL;
1192 printf("# extended partial response\n");
1195 rc = ldap_parse_extended_partial( ld, partial,
1196 &retoid, &retdata, &ctrls, 0 );
1198 if( rc != LDAP_SUCCESS ) {
1199 ldap_perror(ld, "ldap_parse_extended_partial");
1200 exit( EXIT_FAILURE );
1203 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1204 "partial", retoid, retoid ? strlen(retoid) : 0 );
1206 ber_memfree( retoid );
1209 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1211 retdata->bv_val, retdata->bv_len );
1213 ber_bvfree( retdata );
1217 print_ctrls( ctrls );
1218 ldap_controls_free( ctrls );
1222 static int print_result(
1224 LDAPMessage *result, int search )
1228 char *matcheddn = NULL;
1231 LDAPControl **ctrls = NULL;
1235 printf("# search result\n");
1238 printf("%s: %d\n", "search", ldap_msgid(result) );
1242 rc = ldap_parse_result( ld, result,
1243 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1245 if( rc != LDAP_SUCCESS ) {
1246 ldap_perror(ld, "ldap_parse_result");
1247 exit( EXIT_FAILURE );
1252 printf( "result: %d %s\n", err, ldap_err2string(err) );
1254 } else if ( err != LDAP_SUCCESS ) {
1255 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1258 if( matcheddn && *matcheddn ) {
1260 write_ldif( LDIF_PUT_VALUE,
1261 "matchedDN", matcheddn, strlen(matcheddn) );
1263 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1266 ber_memfree( matcheddn );
1269 if( text && *text ) {
1271 write_ldif( LDIF_PUT_TEXT, "text",
1272 text, strlen(text) );
1274 fprintf( stderr, "Additional information: %s\n", text );
1277 ber_memfree( text );
1282 for( i=0; refs[i] != NULL; i++ ) {
1284 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1286 fprintf( stderr, "Referral: %s\n", refs[i] );
1290 ber_memvfree( (void **) refs );
1294 print_ctrls( ctrls );
1295 ldap_controls_free( ctrls );
1301 static void print_ctrls(
1302 LDAPControl **ctrls )
1305 for(i=0; ctrls[i] != NULL; i++ ) {
1306 /* control: OID criticality base64value */
1307 struct berval *b64 = NULL;
1311 len = strlen( ctrls[i]->ldctl_oid );
1313 /* add enough for space after OID and the critical value itself */
1314 len += ctrls[i]->ldctl_iscritical
1315 ? sizeof("true") : sizeof("false");
1317 /* convert to base64 */
1318 if( ctrls[i]->ldctl_value.bv_len ) {
1319 b64 = ber_memalloc( sizeof(struct berval) );
1321 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1322 ctrls[i]->ldctl_value.bv_len ) + 1;
1323 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1325 b64->bv_len = lutil_b64_ntop(
1326 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1327 b64->bv_val, b64->bv_len );
1331 len += 1 + b64->bv_len;
1334 str = malloc( len + 1 );
1335 strcpy( str, ctrls[i]->ldctl_oid );
1336 strcat( str, ctrls[i]->ldctl_iscritical
1337 ? " true" : " false" );
1341 strcat(str, b64->bv_val );
1344 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1345 "control", str, len );
1353 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1357 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1361 fputs( ldif, stdout );
1362 ber_memfree( ldif );