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 " -l limit time limit (in seconds) for search\n"
54 " -L print responses in LDIFv1 format\n"
55 " -LL print responses in LDIF format without comments\n"
56 " -LLL print responses in LDIF format without comments\n"
58 " -s scope one of base, one, or sub (search scope)\n"
59 " -S attr sort the results by attribute `attr'\n"
60 " -t write binary values to files in temporary directory\n"
61 " -tt write all values to files in temporary directory\n"
62 " -T path write files to directory specified by path (default:\n"
64 " -u include User Friendly entry names in the output\n"
65 " -V prefix URL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\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 user SASL authentication identity (username)\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 id 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 *timelimit,
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, line[BUFSIZ];
168 int rc, i, first, scope, deref, attrsonly, manageDSAit;
169 int referrals, timelimit, sizelimit, debug;
170 int authmethod, version, want_bindpw;
174 debug = verbose = not = vals2tmp = referrals =
175 attrsonly = manageDSAit = ldif = want_bindpw = 0;
177 deref = sizelimit = timelimit = version = -1;
179 scope = LDAP_SCOPE_SUBTREE;
182 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
184 while (( i = getopt( argc, argv, "Aa:b:f:Ll:S:s:T:tuV:z:"
185 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z")) != EOF )
189 case 'a': /* set alias deref option */
190 if ( strcasecmp( optarg, "never" ) == 0 ) {
191 deref = LDAP_DEREF_NEVER;
192 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
193 deref = LDAP_DEREF_SEARCHING;
194 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
195 deref = LDAP_DEREF_FINDING;
196 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
197 deref = LDAP_DEREF_ALWAYS;
199 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
203 case 'A': /* retrieve attribute names only -- no values */
206 case 'b': /* search base */
207 base = strdup( optarg );
209 case 'f': /* input file */
210 if( infile != NULL ) {
211 fprintf( stderr, "%s: -f previously specified\n" );
214 infile = strdup( optarg );
216 case 'l': /* time limit */
217 timelimit = atoi( optarg );
219 case 'L': /* print entries in LDIF format */
222 case 's': /* search scope */
223 if ( strcasecmp( optarg, "base" ) == 0 ) {
224 scope = LDAP_SCOPE_BASE;
225 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
226 scope = LDAP_SCOPE_ONELEVEL;
227 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
228 scope = LDAP_SCOPE_SUBTREE;
230 fprintf( stderr, "scope should be base, one, or sub\n" );
234 case 'S': /* sort attribute */
235 sortattr = strdup( optarg );
237 case 'u': /* include UFN */
240 case 't': /* write attribute values to TMPDIR files */
243 case 'T': /* tmpdir */
244 if( tmpdir ) free( tmpdir );
245 tmpdir = strdup( optarg );
247 case 'V': /* uri prefix */
248 if( urlpre ) free( urlpre );
249 urlpre = strdup( optarg );
251 case 'z': /* size limit */
252 sizelimit = atoi( optarg );
260 debug |= atoi( optarg );
262 case 'D': /* bind DN */
263 if( binddn != NULL ) {
264 fprintf( stderr, "%s: -D previously specified\n" );
267 binddn = strdup( optarg );
269 case 'h': /* ldap host */
270 if( ldapuri != NULL ) {
271 fprintf( stderr, "%s: -h incompatible with -H\n" );
274 if( ldaphost != NULL ) {
275 fprintf( stderr, "%s: -h previously specified\n" );
278 ldaphost = strdup( optarg );
280 case 'H': /* ldap URI */
281 if( ldaphost != NULL ) {
282 fprintf( stderr, "%s: -H incompatible with -h\n" );
286 fprintf( stderr, "%s: -H incompatible with -p\n" );
289 if( ldapuri != NULL ) {
290 fprintf( stderr, "%s: -H previously specified\n" );
293 ldapuri = strdup( optarg );
296 #ifdef HAVE_CYRUS_SASL
297 if( version == LDAP_VERSION2 ) {
298 fprintf( stderr, "%s: -I incompatible with version %d\n",
302 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
303 fprintf( stderr, "%s: incompatible previous "
304 "authentication choice\n",
308 authmethod = LDAP_AUTH_SASL;
309 version = LDAP_VERSION3;
310 sasl_flags = LDAP_SASL_INTERACTIVE;
313 fprintf( stderr, "%s: was not compiled with SASL support\n",
315 return( EXIT_FAILURE );
317 case 'k': /* kerberos bind */
318 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
319 if( version > LDAP_VERSION2 ) {
320 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
325 if( authmethod != -1 ) {
326 fprintf( stderr, "%s: -k incompatible with previous "
327 "authentication choice\n", prog );
331 authmethod = LDAP_AUTH_KRBV4;
333 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
337 case 'K': /* kerberos bind, part one only */
338 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
339 if( version > LDAP_VERSION2 ) {
340 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
344 if( authmethod != -1 ) {
345 fprintf( stderr, "%s: incompatible with previous "
346 "authentication choice\n", prog );
350 authmethod = LDAP_AUTH_KRBV41;
352 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
353 return( EXIT_FAILURE );
357 /* enable Manage DSA IT */
358 if( version == LDAP_VERSION2 ) {
359 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
364 version = LDAP_VERSION3;
366 case 'n': /* print deletes, don't actually do them */
370 #ifdef HAVE_CYRUS_SASL
371 if( sasl_secprops != NULL ) {
372 fprintf( stderr, "%s: -O previously specified\n" );
375 if( version == LDAP_VERSION2 ) {
376 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
380 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
381 fprintf( stderr, "%s: incompatible previous "
382 "authentication choice\n", prog );
385 authmethod = LDAP_AUTH_SASL;
386 version = LDAP_VERSION3;
387 sasl_secprops = strdup( optarg );
389 fprintf( stderr, "%s: not compiled with SASL support\n",
391 return( EXIT_FAILURE );
396 fprintf( stderr, "%s: -p previously specified\n" );
399 ldapport = atoi( optarg );
402 switch( atoi(optarg) ) {
404 if( version == LDAP_VERSION3 ) {
405 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
409 version = LDAP_VERSION2;
412 if( version == LDAP_VERSION2 ) {
413 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
417 version = LDAP_VERSION3;
420 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
423 return( EXIT_FAILURE );
426 #ifdef HAVE_CYRUS_SASL
427 if( version == LDAP_VERSION2 ) {
428 fprintf( stderr, "%s: -Q incompatible with version %d\n",
432 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
433 fprintf( stderr, "%s: incompatible previous "
434 "authentication choice\n",
438 authmethod = LDAP_AUTH_SASL;
439 version = LDAP_VERSION3;
440 sasl_flags = LDAP_SASL_QUIET;
443 fprintf( stderr, "%s: not compiled with SASL support\n",
445 return( EXIT_FAILURE );
448 #ifdef HAVE_CYRUS_SASL
449 if( sasl_realm != NULL ) {
450 fprintf( stderr, "%s: -R previously specified\n" );
453 if( version == LDAP_VERSION2 ) {
454 fprintf( stderr, "%s: -R incompatible with version %d\n",
458 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
459 fprintf( stderr, "%s: incompatible previous "
460 "authentication choice\n",
464 authmethod = LDAP_AUTH_SASL;
465 version = LDAP_VERSION3;
466 sasl_realm = strdup( optarg );
468 fprintf( stderr, "%s: not compiled with SASL support\n",
470 return( EXIT_FAILURE );
474 #ifdef HAVE_CYRUS_SASL
475 if( sasl_authc_id != NULL ) {
476 fprintf( stderr, "%s: -U previously specified\n" );
479 if( version == LDAP_VERSION2 ) {
480 fprintf( stderr, "%s: -U incompatible with version %d\n",
484 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
485 fprintf( stderr, "%s: incompatible previous "
486 "authentication choice\n",
490 authmethod = LDAP_AUTH_SASL;
491 version = LDAP_VERSION3;
492 sasl_authc_id = strdup( optarg );
494 fprintf( stderr, "%s: not compiled with SASL support\n",
496 return( EXIT_FAILURE );
499 case 'v': /* verbose mode */
502 case 'w': /* password */
503 passwd.bv_val = strdup( optarg );
507 for( p = optarg; *p == '\0'; p++ ) {
511 passwd.bv_len = strlen( passwd.bv_val );
517 #ifdef HAVE_CYRUS_SASL
518 if( sasl_mech != NULL ) {
519 fprintf( stderr, "%s: -Y previously specified\n" );
522 if( version == LDAP_VERSION2 ) {
523 fprintf( stderr, "%s: -Y incompatible with version %d\n",
527 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
528 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
531 authmethod = LDAP_AUTH_SASL;
532 version = LDAP_VERSION3;
533 sasl_mech = strdup( optarg );
535 fprintf( stderr, "%s: not compiled with SASL support\n",
537 return( EXIT_FAILURE );
541 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
542 fprintf( stderr, "%s: incompatible with previous "
543 "authentication choice\n", prog );
546 authmethod = LDAP_AUTH_SIMPLE;
549 #ifdef HAVE_CYRUS_SASL
550 if( sasl_authz_id != NULL ) {
551 fprintf( stderr, "%s: -X previously specified\n" );
554 if( version == LDAP_VERSION2 ) {
555 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
559 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
560 fprintf( stderr, "%s: -X incompatible with "
561 "authentication choice\n", prog );
564 authmethod = LDAP_AUTH_SASL;
565 version = LDAP_VERSION3;
566 sasl_authz_id = strdup( optarg );
568 fprintf( stderr, "%s: not compiled with SASL support\n",
570 return( EXIT_FAILURE );
575 if( version == LDAP_VERSION2 ) {
576 fprintf( stderr, "%s: -Z incompatible with version %d\n",
580 version = LDAP_VERSION3;
583 fprintf( stderr, "%s: not compiled with TLS support\n",
585 return( EXIT_FAILURE );
589 fprintf( stderr, "%s: unrecongized option -%c\n",
596 version = LDAP_VERSION3;
598 if (authmethod == -1 && version > LDAP_VERSION2) {
599 #ifdef HAVE_CYRUS_SASL
600 authmethod = LDAP_AUTH_SASL;
602 authmethod = LDAP_AUTH_SIMPLE;
606 if (( argc - optind < 1 ) ||
607 ( *argv[optind] != '(' /*')'*/ &&
608 ( strchr( argv[optind], '=' ) == NULL ) ) )
610 filtpattern = "(objectclass=*)";
612 filtpattern = strdup( argv[optind++] );
615 if ( argv[optind] == NULL ) {
617 } else if ( sortattr == NULL || *sortattr == '\0' ) {
618 attrs = &argv[optind];
621 if ( infile != NULL ) {
622 if ( infile[0] == '-' && infile[1] == '\0' ) {
624 } else if (( fp = fopen( infile, "r" )) == NULL ) {
631 && (tmpdir = getenv("TMPDIR")) == NULL
632 && (tmpdir = getenv("TMP")) == NULL
633 && (tmpdir = getenv("TEMP")) == NULL )
635 tmpdir = LDAP_TMPDIR;
638 if( urlpre == NULL ) {
639 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
641 if( urlpre == NULL ) {
646 sprintf( urlpre, "file:///%s/",
647 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
649 /* urlpre should be URLized.... */
653 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
654 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
656 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
657 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
663 (void) SIGNAL( SIGPIPE, SIG_IGN );
666 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
668 fprintf( stderr, "ldap_init( %s, %d )\n",
669 ldaphost != NULL ? ldaphost : "<DEFAULT>",
673 ld = ldap_init( ldaphost, ldapport );
675 perror("ldapsearch: ldap_init");
681 fprintf( stderr, "ldap_initialize( %s )\n",
682 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
685 rc = ldap_initialize( &ld, ldapuri );
686 if( rc != LDAP_SUCCESS ) {
687 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
688 rc, ldap_err2string(rc) );
694 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
696 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
699 if (timelimit != -1 &&
700 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
702 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
705 if (sizelimit != -1 &&
706 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
708 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
713 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
714 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
716 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
717 referrals ? "on" : "off" );
721 if (version == -1 ) {
725 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
726 != LDAP_OPT_SUCCESS )
728 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
733 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
735 ldap_perror( ld, "ldap_start_tls" );
738 fprintf( stderr, "WARNING: could not start TLS\n" );
742 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
743 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
746 if ( authmethod == LDAP_AUTH_SASL ) {
747 #ifdef HAVE_CYRUS_SASL
750 if( sasl_secprops != NULL ) {
751 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
752 (void *) sasl_secprops );
754 if( rc != LDAP_OPT_SUCCESS ) {
756 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
758 return( EXIT_FAILURE );
762 defaults = lutil_sasl_defaults( ld,
769 rc = ldap_sasl_interactive_bind_s( ld, binddn,
770 sasl_mech, NULL, NULL,
771 sasl_flags, lutil_sasl_interact, defaults );
773 if( rc != LDAP_SUCCESS ) {
774 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
775 return( EXIT_FAILURE );
778 fprintf( stderr, "%s: not compiled with SASL support\n",
780 return( EXIT_FAILURE );
783 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
785 ldap_perror( ld, "ldap_bind" );
786 return( EXIT_FAILURE );
793 LDAPControl *ctrls[2];
797 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
798 c.ldctl_value.bv_val = NULL;
799 c.ldctl_value.bv_len = 0;
800 c.ldctl_iscritical = manageDSAit > 1;
802 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
804 if( err != LDAP_OPT_SUCCESS ) {
805 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
806 c.ldctl_iscritical ? "critical " : "" );
807 if( c.ldctl_iscritical ) {
808 exit( EXIT_FAILURE );
814 fprintf( stderr, "filter%s: %s\nrequesting: ",
815 infile != NULL ? " pattern" : "",
818 if ( attrs == NULL ) {
819 fprintf( stderr, "ALL" );
821 for ( i = 0; attrs[ i ] != NULL; ++i ) {
822 fprintf( stderr, "%s ", attrs[ i ] );
825 fprintf( stderr, "\n" );
829 printf( "version: %d\n\n", ldif ? 1 : 2 );
833 printf( "#\n# filter%s: %s\n# requesting: ",
834 infile != NULL ? " pattern" : "",
837 if ( attrs == NULL ) {
840 for ( i = 0; attrs[ i ] != NULL; ++i ) {
841 printf( "%s ", attrs[ i ] );
846 printf("\n# with manageDSAit %scontrol",
847 manageDSAit > 1 ? "critical " : "" );
853 if ( infile == NULL ) {
854 rc = dosearch( ld, base, scope, NULL, filtpattern,
855 attrs, attrsonly, NULL, NULL, NULL, -1 );
860 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
861 line[ strlen( line ) - 1 ] = '\0';
867 rc = dosearch( ld, base, scope, filtpattern, line,
868 attrs, attrsonly, NULL, NULL, NULL, -1 );
888 LDAPControl **sctrls,
889 LDAPControl **cctrls,
890 struct timeval *timelimit,
893 char filter[ BUFSIZ ];
900 LDAPMessage *res, *msg;
903 if( filtpatt != NULL ) {
904 sprintf( filter, filtpatt, value );
907 fprintf( stderr, "filter is: (%s)\n", filter );
911 printf( "#\n# filter: %s\n#\n", filter );
915 sprintf( filter, "%s", value );
922 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
923 sctrls, cctrls, timelimit, sizelimit, &msgid );
925 if( rc != LDAP_SUCCESS ) {
926 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
927 prog, ldap_err2string( rc ), rc );
931 nresponses = nentries = nreferences = nextended = npartial = 0;
935 while ((rc = ldap_result( ld, LDAP_RES_ANY,
936 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
940 (void) ldap_sort_entries( ld, &res,
941 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
944 for ( msg = ldap_first_message( ld, res );
946 msg = ldap_next_message( ld, msg ) )
948 if( nresponses++ ) putchar('\n');
950 switch( ldap_msgtype( msg ) ) {
951 case LDAP_RES_SEARCH_ENTRY:
953 print_entry( ld, msg, attrsonly );
956 case LDAP_RES_SEARCH_REFERENCE:
958 print_reference( ld, msg );
961 case LDAP_RES_EXTENDED:
963 print_extended( ld, msg );
965 if( ldap_msgid( msg ) == 0 ) {
966 /* unsolicited extended operation */
971 case LDAP_RES_EXTENDED_PARTIAL:
973 print_partial( ld, msg );
976 case LDAP_RES_SEARCH_RESULT:
977 rc = print_result( ld, msg, 1 );
986 ldap_perror( ld, "ldap_result" );
992 printf( "\n# numResponses: %d\n", nresponses );
993 if( nentries ) printf( "# numEntries: %d\n", nentries );
994 if( nextended ) printf( "# numExtended: %d\n", nextended );
995 if( npartial ) printf( "# numPartial: %d\n", npartial );
996 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1009 char tmpfname[ 256 ];
1012 BerElement *ber = NULL;
1013 struct berval **bvals;
1014 LDAPControl **ctrls = NULL;
1017 dn = ldap_get_dn( ld, entry );
1021 ufn = ldap_dn2ufn( dn );
1022 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1024 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1026 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1028 if( rc != LDAP_SUCCESS ) {
1029 fprintf(stderr, "print_entry: %d\n", rc );
1030 ldap_perror( ld, "ldap_get_entry_controls" );
1031 exit( EXIT_FAILURE );
1035 print_ctrls( ctrls );
1036 ldap_controls_free( ctrls );
1041 ufn = ldap_dn2ufn( dn );
1043 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1046 if( ufn != NULL ) ldap_memfree( ufn );
1049 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1050 a = ldap_next_attribute( ld, entry, ber ) )
1053 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1055 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1056 for ( i = 0; bvals[i] != NULL; i++ ) {
1057 if ( vals2tmp > 1 || ( vals2tmp
1058 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1061 /* write value to file */
1062 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1066 if ( mktemp( tmpfname ) == NULL ) {
1071 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1076 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1081 if ( fwrite( bvals[ i ]->bv_val,
1082 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1091 sprintf( url, "%s%s", urlpre,
1092 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1094 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1097 write_ldif( LDIF_PUT_VALUE, a,
1098 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1101 ber_bvecfree( bvals );
1110 static void print_reference(
1112 LDAPMessage *reference )
1116 LDAPControl **ctrls;
1119 printf("# search reference\n");
1122 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1124 if( rc != LDAP_SUCCESS ) {
1125 ldap_perror(ld, "ldap_parse_reference");
1126 exit( EXIT_FAILURE );
1131 for( i=0; refs[i] != NULL; i++ ) {
1132 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1133 "ref", refs[i], strlen(refs[i]) );
1135 ber_memvfree( (void **) refs );
1139 print_ctrls( ctrls );
1140 ldap_controls_free( ctrls );
1144 static void print_extended(
1146 LDAPMessage *extended )
1149 char *retoid = NULL;
1150 struct berval *retdata = NULL;
1153 printf("# extended result response\n");
1156 rc = ldap_parse_extended_result( ld, extended,
1157 &retoid, &retdata, 0 );
1159 if( rc != LDAP_SUCCESS ) {
1160 ldap_perror(ld, "ldap_parse_extended_result");
1161 exit( EXIT_FAILURE );
1164 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1165 "extended", retoid, retoid ? strlen(retoid) : 0 );
1166 ber_memfree( retoid );
1169 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1170 "data", retdata->bv_val, retdata->bv_len );
1171 ber_bvfree( retdata );
1174 print_result( ld, extended, 0 );
1177 static void print_partial(
1179 LDAPMessage *partial )
1182 char *retoid = NULL;
1183 struct berval *retdata = NULL;
1184 LDAPControl **ctrls = NULL;
1187 printf("# extended partial response\n");
1190 rc = ldap_parse_extended_partial( ld, partial,
1191 &retoid, &retdata, &ctrls, 0 );
1193 if( rc != LDAP_SUCCESS ) {
1194 ldap_perror(ld, "ldap_parse_extended_partial");
1195 exit( EXIT_FAILURE );
1198 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1199 "partial", retoid, retoid ? strlen(retoid) : 0 );
1201 ber_memfree( retoid );
1204 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1206 retdata->bv_val, retdata->bv_len );
1208 ber_bvfree( retdata );
1212 print_ctrls( ctrls );
1213 ldap_controls_free( ctrls );
1217 static int print_result(
1219 LDAPMessage *result, int search )
1224 char *matcheddn = NULL;
1227 LDAPControl **ctrls = NULL;
1231 printf("# search result\n");
1234 printf("%s: %d\n", "search", ldap_msgid(result) );
1238 rc = ldap_parse_result( ld, result,
1239 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1241 if( rc != LDAP_SUCCESS ) {
1242 ldap_perror(ld, "ldap_parse_result");
1243 exit( EXIT_FAILURE );
1248 printf( "result: %d %s\n", err, ldap_err2string(err) );
1250 } else if ( err != LDAP_SUCCESS ) {
1251 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1254 if( matcheddn && *matcheddn ) {
1256 write_ldif( LDIF_PUT_VALUE,
1257 "matchedDN", matcheddn, strlen(matcheddn) );
1259 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1262 ber_memfree( matcheddn );
1265 if( text && *text ) {
1267 write_ldif( LDIF_PUT_TEXT, "text",
1268 text, strlen(text) );
1270 fprintf( stderr, "Additional information: %s\n", text );
1273 ber_memfree( text );
1278 for( i=0; refs[i] != NULL; i++ ) {
1280 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1282 fprintf( stderr, "Referral: %s\n", refs[i] );
1286 ber_memvfree( (void **) refs );
1290 print_ctrls( ctrls );
1291 ldap_controls_free( ctrls );
1297 void print_ctrls( LDAPControl **ctrls ) {
1299 for(i=0; ctrls[i] != NULL; i++ ) {
1300 /* control: OID criticality base64value */
1301 struct berval *b64 = NULL;
1305 len = strlen( ctrls[i]->ldctl_oid );
1307 /* add enough for space after OID and the critical value itself */
1308 len += ctrls[i]->ldctl_iscritical
1309 ? sizeof("true") : sizeof("false");
1311 /* convert to base64 */
1312 if( ctrls[i]->ldctl_value.bv_len ) {
1313 b64 = ber_memalloc( sizeof(struct berval) );
1315 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1316 ctrls[i]->ldctl_value.bv_len ) + 1;
1317 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1319 b64->bv_len = lutil_b64_ntop(
1320 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1321 b64->bv_val, b64->bv_len );
1325 len += 1 + b64->bv_len;
1328 str = malloc( len + 1 );
1329 strcpy( str, ctrls[i]->ldctl_oid );
1330 strcat( str, ctrls[i]->ldctl_iscritical
1331 ? " true" : " false" );
1335 strcat(str, b64->bv_val );
1338 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1339 "control", str, len );
1347 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1351 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1355 fputs( ldif, stdout );
1356 ber_memfree( ldif );