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",
740 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
741 ldap_perror( ld, "ldap_start_tls" );
748 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
749 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
752 if ( authmethod == LDAP_AUTH_SASL ) {
753 #ifdef HAVE_CYRUS_SASL
756 if( sasl_secprops != NULL ) {
757 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
758 (void *) sasl_secprops );
760 if( rc != LDAP_OPT_SUCCESS ) {
762 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
764 return( EXIT_FAILURE );
768 defaults = lutil_sasl_defaults( ld,
775 rc = ldap_sasl_interactive_bind_s( ld, binddn,
776 sasl_mech, NULL, NULL,
777 sasl_flags, lutil_sasl_interact, defaults );
779 if( rc != LDAP_SUCCESS ) {
780 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
781 return( EXIT_FAILURE );
784 fprintf( stderr, "%s: not compiled with SASL support\n",
786 return( EXIT_FAILURE );
789 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
791 ldap_perror( ld, "ldap_bind" );
792 return( EXIT_FAILURE );
799 LDAPControl *ctrls[2];
803 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
804 c.ldctl_value.bv_val = NULL;
805 c.ldctl_value.bv_len = 0;
806 c.ldctl_iscritical = manageDSAit > 1;
808 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
810 if( err != LDAP_OPT_SUCCESS ) {
811 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
812 c.ldctl_iscritical ? "critical " : "" );
813 if( c.ldctl_iscritical ) {
814 exit( EXIT_FAILURE );
820 fprintf( stderr, "filter%s: %s\nrequesting: ",
821 infile != NULL ? " pattern" : "",
824 if ( attrs == NULL ) {
825 fprintf( stderr, "ALL" );
827 for ( i = 0; attrs[ i ] != NULL; ++i ) {
828 fprintf( stderr, "%s ", attrs[ i ] );
831 fprintf( stderr, "\n" );
835 printf( "version: %d\n\n", ldif ? 1 : 2 );
839 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
841 infile != NULL ? " pattern" : "",
844 if ( attrs == NULL ) {
847 for ( i = 0; attrs[ i ] != NULL; ++i ) {
848 printf( "%s ", attrs[ i ] );
853 printf("\n# with manageDSAit %scontrol",
854 manageDSAit > 1 ? "critical " : "" );
860 if ( infile == NULL ) {
861 rc = dosearch( ld, base, scope, NULL, filtpattern,
862 attrs, attrsonly, NULL, NULL, NULL, -1 );
867 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
868 line[ strlen( line ) - 1 ] = '\0';
874 rc = dosearch( ld, base, scope, filtpattern, line,
875 attrs, attrsonly, NULL, NULL, NULL, -1 );
895 LDAPControl **sctrls,
896 LDAPControl **cctrls,
897 struct timeval *timeout,
900 char filter[ BUFSIZ ];
907 LDAPMessage *res, *msg;
910 if( filtpatt != NULL ) {
911 sprintf( filter, filtpatt, value );
914 fprintf( stderr, "filter: %s\n", filter );
918 printf( "#\n# filter: %s\n#\n", filter );
922 sprintf( filter, "%s", value );
929 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
930 sctrls, cctrls, timeout, sizelimit, &msgid );
932 if( rc != LDAP_SUCCESS ) {
933 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
934 prog, ldap_err2string( rc ), rc );
938 nresponses = nentries = nreferences = nextended = npartial = 0;
942 while ((rc = ldap_result( ld, LDAP_RES_ANY,
943 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
947 (void) ldap_sort_entries( ld, &res,
948 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
951 for ( msg = ldap_first_message( ld, res );
953 msg = ldap_next_message( ld, msg ) )
955 if( nresponses++ ) putchar('\n');
957 switch( ldap_msgtype( msg ) ) {
958 case LDAP_RES_SEARCH_ENTRY:
960 print_entry( ld, msg, attrsonly );
963 case LDAP_RES_SEARCH_REFERENCE:
965 print_reference( ld, msg );
968 case LDAP_RES_EXTENDED:
970 print_extended( ld, msg );
972 if( ldap_msgid( msg ) == 0 ) {
973 /* unsolicited extended operation */
978 case LDAP_RES_EXTENDED_PARTIAL:
980 print_partial( ld, msg );
983 case LDAP_RES_SEARCH_RESULT:
984 rc = print_result( ld, msg, 1 );
993 ldap_perror( ld, "ldap_result" );
999 printf( "\n# numResponses: %d\n", nresponses );
1000 if( nentries ) printf( "# numEntries: %d\n", nentries );
1001 if( nextended ) printf( "# numExtended: %d\n", nextended );
1002 if( npartial ) printf( "# numPartial: %d\n", npartial );
1003 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1016 char tmpfname[ 256 ];
1019 BerElement *ber = NULL;
1020 struct berval **bvals;
1021 LDAPControl **ctrls = NULL;
1024 dn = ldap_get_dn( ld, entry );
1028 ufn = ldap_dn2ufn( dn );
1029 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1031 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1033 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1035 if( rc != LDAP_SUCCESS ) {
1036 fprintf(stderr, "print_entry: %d\n", rc );
1037 ldap_perror( ld, "ldap_get_entry_controls" );
1038 exit( EXIT_FAILURE );
1042 print_ctrls( ctrls );
1043 ldap_controls_free( ctrls );
1048 ufn = ldap_dn2ufn( dn );
1050 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1053 if( ufn != NULL ) ldap_memfree( ufn );
1056 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1057 a = ldap_next_attribute( ld, entry, ber ) )
1060 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1062 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1063 for ( i = 0; bvals[i] != NULL; i++ ) {
1064 if ( vals2tmp > 1 || ( vals2tmp
1065 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1068 /* write value to file */
1069 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1073 if ( mktemp( tmpfname ) == NULL ) {
1078 tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 );
1079 if ( tmpfd == -1 ) {
1084 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1089 if ( fwrite( bvals[ i ]->bv_val,
1090 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1099 sprintf( url, "%s%s", urlpre,
1100 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1102 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1105 write_ldif( LDIF_PUT_VALUE, a,
1106 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1109 ber_bvecfree( bvals );
1118 static void print_reference(
1120 LDAPMessage *reference )
1124 LDAPControl **ctrls;
1127 printf("# search reference\n");
1130 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1132 if( rc != LDAP_SUCCESS ) {
1133 ldap_perror(ld, "ldap_parse_reference");
1134 exit( EXIT_FAILURE );
1139 for( i=0; refs[i] != NULL; i++ ) {
1140 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1141 "ref", refs[i], strlen(refs[i]) );
1143 ber_memvfree( (void **) refs );
1147 print_ctrls( ctrls );
1148 ldap_controls_free( ctrls );
1152 static void print_extended(
1154 LDAPMessage *extended )
1157 char *retoid = NULL;
1158 struct berval *retdata = NULL;
1161 printf("# extended result response\n");
1164 rc = ldap_parse_extended_result( ld, extended,
1165 &retoid, &retdata, 0 );
1167 if( rc != LDAP_SUCCESS ) {
1168 ldap_perror(ld, "ldap_parse_extended_result");
1169 exit( EXIT_FAILURE );
1172 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1173 "extended", retoid, retoid ? strlen(retoid) : 0 );
1174 ber_memfree( retoid );
1177 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1178 "data", retdata->bv_val, retdata->bv_len );
1179 ber_bvfree( retdata );
1182 print_result( ld, extended, 0 );
1185 static void print_partial(
1187 LDAPMessage *partial )
1190 char *retoid = NULL;
1191 struct berval *retdata = NULL;
1192 LDAPControl **ctrls = NULL;
1195 printf("# extended partial response\n");
1198 rc = ldap_parse_extended_partial( ld, partial,
1199 &retoid, &retdata, &ctrls, 0 );
1201 if( rc != LDAP_SUCCESS ) {
1202 ldap_perror(ld, "ldap_parse_extended_partial");
1203 exit( EXIT_FAILURE );
1206 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1207 "partial", retoid, retoid ? strlen(retoid) : 0 );
1209 ber_memfree( retoid );
1212 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1214 retdata->bv_val, retdata->bv_len );
1216 ber_bvfree( retdata );
1220 print_ctrls( ctrls );
1221 ldap_controls_free( ctrls );
1225 static int print_result(
1227 LDAPMessage *result, int search )
1231 char *matcheddn = NULL;
1234 LDAPControl **ctrls = NULL;
1238 printf("# search result\n");
1241 printf("%s: %d\n", "search", ldap_msgid(result) );
1245 rc = ldap_parse_result( ld, result,
1246 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1248 if( rc != LDAP_SUCCESS ) {
1249 ldap_perror(ld, "ldap_parse_result");
1250 exit( EXIT_FAILURE );
1255 printf( "result: %d %s\n", err, ldap_err2string(err) );
1257 } else if ( err != LDAP_SUCCESS ) {
1258 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1261 if( matcheddn && *matcheddn ) {
1263 write_ldif( LDIF_PUT_VALUE,
1264 "matchedDN", matcheddn, strlen(matcheddn) );
1266 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1269 ber_memfree( matcheddn );
1272 if( text && *text ) {
1274 write_ldif( LDIF_PUT_TEXT, "text",
1275 text, strlen(text) );
1277 fprintf( stderr, "Additional information: %s\n", text );
1280 ber_memfree( text );
1285 for( i=0; refs[i] != NULL; i++ ) {
1287 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1289 fprintf( stderr, "Referral: %s\n", refs[i] );
1293 ber_memvfree( (void **) refs );
1297 print_ctrls( ctrls );
1298 ldap_controls_free( ctrls );
1304 static void print_ctrls(
1305 LDAPControl **ctrls )
1308 for(i=0; ctrls[i] != NULL; i++ ) {
1309 /* control: OID criticality base64value */
1310 struct berval *b64 = NULL;
1314 len = strlen( ctrls[i]->ldctl_oid );
1316 /* add enough for space after OID and the critical value itself */
1317 len += ctrls[i]->ldctl_iscritical
1318 ? sizeof("true") : sizeof("false");
1320 /* convert to base64 */
1321 if( ctrls[i]->ldctl_value.bv_len ) {
1322 b64 = ber_memalloc( sizeof(struct berval) );
1324 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1325 ctrls[i]->ldctl_value.bv_len ) + 1;
1326 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1328 b64->bv_len = lutil_b64_ntop(
1329 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1330 b64->bv_val, b64->bv_len );
1334 len += 1 + b64->bv_len;
1337 str = malloc( len + 1 );
1338 strcpy( str, ctrls[i]->ldctl_oid );
1339 strcat( str, ctrls[i]->ldctl_iscritical
1340 ? " true" : " false" );
1344 strcat(str, b64->bv_val );
1347 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1348 "control", str, len );
1356 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1360 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1364 fputs( ldif, stdout );
1365 ber_memfree( ldif );