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 " -I use SASL Interactive mode\n"
74 " -k use Kerberos authentication\n"
75 " -K like -k, but do only step 1 of the Kerberos bind\n"
76 " -M enable Manage DSA IT control (-MM to make critical)\n"
77 " -n show what would be done but don't actually search\n"
78 " -O props SASL security properties\n"
79 " -p port port on LDAP server\n"
80 " -P version procotol version (default: 3)\n"
81 " -Q use SASL Quiet mode\n"
82 " -R realm SASL realm\n"
83 " -U user SASL authentication identity (username)\n"
84 " -v run in verbose mode (diagnostics to standard output)\n"
85 " -w passwd bind passwd (for simple authentication)\n"
86 " -W prompt for bind passwd\n"
87 " -x Simple authentication\n"
88 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
89 " -Y mech SASL mechanism\n"
90 " -Z Start TLS request (-ZZ to require successful response)\n"
96 static void print_entry LDAP_P((
101 static void print_reference(
103 LDAPMessage *reference );
105 static void print_extended(
107 LDAPMessage *extended );
109 static void print_partial(
111 LDAPMessage *partial );
113 static int print_result(
118 static void print_ctrls(
119 LDAPControl **ctrls );
121 static int write_ldif LDAP_P((
127 static int dosearch LDAP_P((
135 LDAPControl **sctrls,
136 LDAPControl **cctrls,
137 struct timeval *timelimit,
140 static char *tmpdir = NULL;
141 static char *urlpre = NULL;
143 static char *prog = NULL;
144 static char *binddn = NULL;
145 static struct berval passwd = { 0, NULL };
146 static char *base = NULL;
147 static char *ldaphost = NULL;
148 static char *ldapuri = NULL;
149 static int ldapport = 0;
150 #ifdef HAVE_CYRUS_SASL
151 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
152 static char *sasl_realm = NULL;
153 static char *sasl_authc_id = NULL;
154 static char *sasl_authz_id = NULL;
155 static char *sasl_mech = NULL;
156 static char *sasl_secprops = NULL;
158 static int use_tls = 0;
159 static char *sortattr = NULL;
160 static int verbose, not, includeufn, vals2tmp, ldif;
163 main( int argc, char **argv )
165 char *infile, *filtpattern, **attrs, line[BUFSIZ];
167 int rc, i, first, scope, deref, attrsonly, manageDSAit;
168 int referrals, timelimit, sizelimit, debug;
169 int authmethod, version, want_bindpw;
173 debug = verbose = not = vals2tmp = referrals =
174 attrsonly = manageDSAit = ldif = want_bindpw = 0;
176 deref = sizelimit = timelimit = version = -1;
178 scope = LDAP_SCOPE_SUBTREE;
181 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
183 while (( i = getopt( argc, argv,
184 "Aa:b:f:Ll:S:s:T:tuV:z:" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z")) != EOF )
188 case 'a': /* set alias deref option */
189 if ( strcasecmp( optarg, "never" ) == 0 ) {
190 deref = LDAP_DEREF_NEVER;
191 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
192 deref = LDAP_DEREF_SEARCHING;
193 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
194 deref = LDAP_DEREF_FINDING;
195 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
196 deref = LDAP_DEREF_ALWAYS;
198 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
202 case 'A': /* retrieve attribute names only -- no values */
205 case 'b': /* search base */
206 base = strdup( optarg );
208 case 'f': /* input file */
209 if( infile != NULL ) {
210 fprintf( stderr, "%s: -f previously specified\n" );
213 infile = strdup( optarg );
215 case 'l': /* time limit */
216 timelimit = atoi( optarg );
218 case 'L': /* print entries in LDIF format */
221 case 's': /* search scope */
222 if ( strcasecmp( optarg, "base" ) == 0 ) {
223 scope = LDAP_SCOPE_BASE;
224 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
225 scope = LDAP_SCOPE_ONELEVEL;
226 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
227 scope = LDAP_SCOPE_SUBTREE;
229 fprintf( stderr, "scope should be base, one, or sub\n" );
233 case 'S': /* sort attribute */
234 sortattr = strdup( optarg );
236 case 'u': /* include UFN */
239 case 't': /* write attribute values to TMPDIR files */
242 case 'T': /* tmpdir */
243 if( tmpdir ) free( tmpdir );
244 tmpdir = strdup( optarg );
246 case 'V': /* uri prefix */
247 if( urlpre ) free( urlpre );
248 urlpre = strdup( optarg );
250 case 'z': /* size limit */
251 sizelimit = atoi( optarg );
259 debug |= atoi( optarg );
261 case 'D': /* bind DN */
262 if( binddn != NULL ) {
263 fprintf( stderr, "%s: -D previously specified\n" );
266 binddn = strdup( optarg );
268 case 'h': /* ldap host */
269 if( ldapuri != NULL ) {
270 fprintf( stderr, "%s: -h incompatible with -H\n" );
273 if( ldaphost != NULL ) {
274 fprintf( stderr, "%s: -h previously specified\n" );
277 ldaphost = strdup( optarg );
279 case 'H': /* ldap URI */
280 if( ldaphost != NULL ) {
281 fprintf( stderr, "%s: -H incompatible with -h\n" );
285 fprintf( stderr, "%s: -H incompatible with -p\n" );
288 if( ldapuri != NULL ) {
289 fprintf( stderr, "%s: -H previously specified\n" );
292 ldapuri = strdup( optarg );
295 #ifdef HAVE_CYRUS_SASL
296 if( version == LDAP_VERSION2 ) {
297 fprintf( stderr, "%s: -I incompatible with version %d\n",
301 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
302 fprintf( stderr, "%s: incompatible previous "
303 "authentication choice\n",
307 authmethod = LDAP_AUTH_SASL;
308 version = LDAP_VERSION3;
309 sasl_flags = LDAP_SASL_INTERACTIVE;
312 fprintf( stderr, "%s: was not compiled with SASL support\n",
314 return( EXIT_FAILURE );
316 case 'k': /* kerberos bind */
317 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
318 if( version > LDAP_VERSION2 ) {
319 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
324 if( authmethod != -1 ) {
325 fprintf( stderr, "%s: -k incompatible with previous "
326 "authentication choice\n", prog );
330 authmethod = LDAP_AUTH_KRBV4;
332 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
336 case 'K': /* kerberos bind, part one only */
337 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
338 if( version > LDAP_VERSION2 ) {
339 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
343 if( authmethod != -1 ) {
344 fprintf( stderr, "%s: incompatible with previous "
345 "authentication choice\n", prog );
349 authmethod = LDAP_AUTH_KRBV41;
351 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
352 return( EXIT_FAILURE );
356 /* enable Manage DSA IT */
357 if( version == LDAP_VERSION2 ) {
358 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
363 version = LDAP_VERSION3;
365 case 'n': /* print deletes, don't actually do them */
369 #ifdef HAVE_CYRUS_SASL
370 if( sasl_secprops != NULL ) {
371 fprintf( stderr, "%s: -O previously specified\n" );
374 if( version == LDAP_VERSION2 ) {
375 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
379 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
380 fprintf( stderr, "%s: incompatible previous "
381 "authentication choice\n", prog );
384 authmethod = LDAP_AUTH_SASL;
385 version = LDAP_VERSION3;
386 sasl_secprops = strdup( optarg );
388 fprintf( stderr, "%s: not compiled with SASL support\n",
390 return( EXIT_FAILURE );
395 fprintf( stderr, "%s: -p previously specified\n" );
398 ldapport = atoi( optarg );
401 switch( atoi(optarg) ) {
403 if( version == LDAP_VERSION3 ) {
404 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
408 version = LDAP_VERSION2;
411 if( version == LDAP_VERSION2 ) {
412 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
416 version = LDAP_VERSION3;
419 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
422 return( EXIT_FAILURE );
425 #ifdef HAVE_CYRUS_SASL
426 if( version == LDAP_VERSION2 ) {
427 fprintf( stderr, "%s: -Q incompatible with version %d\n",
431 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
432 fprintf( stderr, "%s: incompatible previous "
433 "authentication choice\n",
437 authmethod = LDAP_AUTH_SASL;
438 version = LDAP_VERSION3;
439 sasl_flags = LDAP_SASL_QUIET;
442 fprintf( stderr, "%s: not compiled with SASL support\n",
444 return( EXIT_FAILURE );
447 #ifdef HAVE_CYRUS_SASL
448 if( sasl_realm != NULL ) {
449 fprintf( stderr, "%s: -R previously specified\n" );
452 if( version == LDAP_VERSION2 ) {
453 fprintf( stderr, "%s: -R incompatible with version %d\n",
457 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
458 fprintf( stderr, "%s: incompatible previous "
459 "authentication choice\n",
463 authmethod = LDAP_AUTH_SASL;
464 version = LDAP_VERSION3;
465 sasl_realm = strdup( optarg );
467 fprintf( stderr, "%s: not compiled with SASL support\n",
469 return( EXIT_FAILURE );
473 #ifdef HAVE_CYRUS_SASL
474 if( sasl_authc_id != NULL ) {
475 fprintf( stderr, "%s: -U previously specified\n" );
478 if( version == LDAP_VERSION2 ) {
479 fprintf( stderr, "%s: -U incompatible with version %d\n",
483 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
484 fprintf( stderr, "%s: incompatible previous "
485 "authentication choice\n",
489 authmethod = LDAP_AUTH_SASL;
490 version = LDAP_VERSION3;
491 sasl_authc_id = strdup( optarg );
493 fprintf( stderr, "%s: not compiled with SASL support\n",
495 return( EXIT_FAILURE );
498 case 'v': /* verbose mode */
501 case 'w': /* password */
502 passwd.bv_val = strdup( optarg );
506 for( p = optarg; *p == '\0'; p++ ) {
510 passwd.bv_len = strlen( passwd.bv_val );
516 #ifdef HAVE_CYRUS_SASL
517 if( sasl_mech != NULL ) {
518 fprintf( stderr, "%s: -Y previously specified\n" );
521 if( version == LDAP_VERSION2 ) {
522 fprintf( stderr, "%s: -Y incompatible with version %d\n",
526 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
527 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
530 authmethod = LDAP_AUTH_SASL;
531 version = LDAP_VERSION3;
532 sasl_mech = strdup( optarg );
534 fprintf( stderr, "%s: not compiled with SASL support\n",
536 return( EXIT_FAILURE );
540 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
541 fprintf( stderr, "%s: incompatible with previous "
542 "authentication choice\n", prog );
545 authmethod = LDAP_AUTH_SIMPLE;
548 #ifdef HAVE_CYRUS_SASL
549 if( sasl_authz_id != NULL ) {
550 fprintf( stderr, "%s: -X previously specified\n" );
553 if( version == LDAP_VERSION2 ) {
554 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
558 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
559 fprintf( stderr, "%s: -X incompatible with "
560 "authentication choice\n", prog );
563 authmethod = LDAP_AUTH_SASL;
564 version = LDAP_VERSION3;
565 sasl_authz_id = strdup( optarg );
567 fprintf( stderr, "%s: not compiled with SASL support\n",
569 return( EXIT_FAILURE );
574 if( version == LDAP_VERSION2 ) {
575 fprintf( stderr, "%s: -Z incompatible with version %d\n",
579 version = LDAP_VERSION3;
582 fprintf( stderr, "%s: not compiled with TLS support\n",
584 return( EXIT_FAILURE );
588 fprintf( stderr, "%s: unrecongized option -%c\n",
595 version = LDAP_VERSION3;
597 if (authmethod == -1 && version > LDAP_VERSION2) {
598 #ifdef HAVE_CYRUS_SASL
599 authmethod = LDAP_AUTH_SASL;
601 authmethod = LDAP_AUTH_SIMPLE;
605 if (( argc - optind < 1 ) ||
606 ( *argv[optind] != '(' /*')'*/ &&
607 ( strchr( argv[optind], '=' ) == NULL ) ) )
609 filtpattern = "(objectclass=*)";
611 filtpattern = strdup( argv[optind++] );
614 if ( argv[optind] == NULL ) {
616 } else if ( sortattr == NULL || *sortattr == '\0' ) {
617 attrs = &argv[optind];
620 if ( infile != NULL ) {
621 if ( infile[0] == '-' && infile[1] == '\0' ) {
623 } else if (( fp = fopen( infile, "r" )) == NULL ) {
630 && (tmpdir = getenv("TMPDIR")) == NULL
631 && (tmpdir = getenv("TMP")) == NULL
632 && (tmpdir = getenv("TEMP")) == NULL )
634 tmpdir = LDAP_TMPDIR;
637 if( urlpre == NULL ) {
638 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
640 if( urlpre == NULL ) {
645 sprintf( urlpre, "file:///%s/",
646 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
648 /* urlpre should be URLized.... */
652 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
653 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
655 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
656 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
662 (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>",
672 ld = ldap_init( ldaphost, ldapport );
676 fprintf( stderr, "ldap_initialize( %s )\n",
677 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
679 (void) ldap_initialize( &ld, ldapuri );
683 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n", rc );
688 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
690 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
693 if (timelimit != -1 &&
694 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
696 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
699 if (sizelimit != -1 &&
700 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
702 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
707 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
708 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
710 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
711 referrals ? "on" : "off" );
715 if (version == -1 ) {
719 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
720 != LDAP_OPT_SUCCESS )
722 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
727 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
729 ldap_perror( ld, "ldap_start_tls" );
732 fprintf( stderr, "WARNING: could not start TLS\n" );
736 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
737 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
740 if ( authmethod == LDAP_AUTH_SASL ) {
741 #ifdef HAVE_CYRUS_SASL
744 if( sasl_secprops != NULL ) {
745 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
746 (void *) sasl_secprops );
748 if( rc != LDAP_OPT_SUCCESS ) {
750 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
752 return( EXIT_FAILURE );
756 defaults = lutil_sasl_defaults( ld,
763 rc = ldap_sasl_interactive_bind_s( ld, binddn,
764 sasl_mech, NULL, NULL,
765 sasl_flags, lutil_sasl_interact, defaults );
767 if( rc != LDAP_SUCCESS ) {
768 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
769 return( EXIT_FAILURE );
772 fprintf( stderr, "%s: not compiled with SASL support\n",
774 return( EXIT_FAILURE );
777 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
779 ldap_perror( ld, "ldap_bind" );
780 return( EXIT_FAILURE );
787 LDAPControl *ctrls[2];
791 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
792 c.ldctl_value.bv_val = NULL;
793 c.ldctl_value.bv_len = 0;
794 c.ldctl_iscritical = manageDSAit > 1;
796 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
798 if( err != LDAP_OPT_SUCCESS ) {
799 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
800 c.ldctl_iscritical ? "critical " : "" );
801 if( c.ldctl_iscritical ) {
802 exit( EXIT_FAILURE );
808 fprintf( stderr, "filter%s: %s\nrequesting: ",
809 infile != NULL ? " pattern" : "",
812 if ( attrs == NULL ) {
813 fprintf( stderr, "ALL" );
815 for ( i = 0; attrs[ i ] != NULL; ++i ) {
816 fprintf( stderr, "%s ", attrs[ i ] );
819 fprintf( stderr, "\n" );
823 printf( "version: %d\n\n", ldif ? 1 : 2 );
827 printf( "#\n# filter%s: %s\n# requesting: ",
828 infile != NULL ? " pattern" : "",
831 if ( attrs == NULL ) {
834 for ( i = 0; attrs[ i ] != NULL; ++i ) {
835 printf( "%s ", attrs[ i ] );
840 printf("\n# with manageDSAit %scontrol",
841 manageDSAit > 1 ? "critical " : "" );
847 if ( infile == NULL ) {
848 rc = dosearch( ld, base, scope, NULL, filtpattern,
849 attrs, attrsonly, NULL, NULL, NULL, -1 );
854 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
855 line[ strlen( line ) - 1 ] = '\0';
861 rc = dosearch( ld, base, scope, filtpattern, line,
862 attrs, attrsonly, NULL, NULL, NULL, -1 );
882 LDAPControl **sctrls,
883 LDAPControl **cctrls,
884 struct timeval *timelimit,
887 char filter[ BUFSIZ ];
894 LDAPMessage *res, *msg;
897 if( filtpatt != NULL ) {
898 sprintf( filter, filtpatt, value );
901 fprintf( stderr, "filter is: (%s)\n", filter );
905 printf( "#\n# filter: %s\n#\n", filter );
909 sprintf( filter, "%s", value );
916 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
917 sctrls, cctrls, timelimit, sizelimit, &msgid );
919 if( rc != LDAP_SUCCESS ) {
920 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
921 prog, ldap_err2string( rc ), rc );
925 nresponses = nentries = nreferences = nextended = npartial = 0;
929 while ((rc = ldap_result( ld, LDAP_RES_ANY,
930 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
934 (void) ldap_sort_entries( ld, &res,
935 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
938 for ( msg = ldap_first_message( ld, res );
940 msg = ldap_next_message( ld, msg ) )
942 if( nresponses++ ) putchar('\n');
944 switch( ldap_msgtype( msg ) ) {
945 case LDAP_RES_SEARCH_ENTRY:
947 print_entry( ld, msg, attrsonly );
950 case LDAP_RES_SEARCH_REFERENCE:
952 print_reference( ld, msg );
955 case LDAP_RES_EXTENDED:
957 print_extended( ld, msg );
959 if( ldap_msgid( msg ) == 0 ) {
960 /* unsolicited extended operation */
965 case LDAP_RES_EXTENDED_PARTIAL:
967 print_partial( ld, msg );
970 case LDAP_RES_SEARCH_RESULT:
971 rc = print_result( ld, msg, 1 );
980 ldap_perror( ld, "ldap_result" );
986 printf( "\n# numResponses: %d\n", nresponses );
987 if( nentries ) printf( "# numEntries: %d\n", nentries );
988 if( nextended ) printf( "# numExtended: %d\n", nextended );
989 if( npartial ) printf( "# numPartial: %d\n", npartial );
990 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1003 char tmpfname[ 256 ];
1006 BerElement *ber = NULL;
1007 struct berval **bvals;
1008 LDAPControl **ctrls = NULL;
1011 dn = ldap_get_dn( ld, entry );
1015 ufn = ldap_dn2ufn( dn );
1016 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1018 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1020 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1022 if( rc != LDAP_SUCCESS ) {
1023 fprintf(stderr, "print_entry: %d\n", rc );
1024 ldap_perror( ld, "ldap_get_entry_controls" );
1025 exit( EXIT_FAILURE );
1029 print_ctrls( ctrls );
1030 ldap_controls_free( ctrls );
1035 ufn = ldap_dn2ufn( dn );
1037 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1040 if( ufn != NULL ) ldap_memfree( ufn );
1043 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1044 a = ldap_next_attribute( ld, entry, ber ) )
1047 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1049 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1050 for ( i = 0; bvals[i] != NULL; i++ ) {
1051 if ( vals2tmp > 1 || ( vals2tmp
1052 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1055 /* write value to file */
1056 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1060 if ( mktemp( tmpfname ) == NULL ) {
1065 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1070 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1075 if ( fwrite( bvals[ i ]->bv_val,
1076 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1085 sprintf( url, "%s%s", urlpre,
1086 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1088 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1091 write_ldif( LDIF_PUT_VALUE, a,
1092 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1095 ber_bvecfree( bvals );
1104 static void print_reference(
1106 LDAPMessage *reference )
1110 LDAPControl **ctrls;
1113 printf("# search reference\n");
1116 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1118 if( rc != LDAP_SUCCESS ) {
1119 ldap_perror(ld, "ldap_parse_reference");
1120 exit( EXIT_FAILURE );
1125 for( i=0; refs[i] != NULL; i++ ) {
1126 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1127 "ref", refs[i], strlen(refs[i]) );
1129 ber_memvfree( (void **) refs );
1133 print_ctrls( ctrls );
1134 ldap_controls_free( ctrls );
1138 static void print_extended(
1140 LDAPMessage *extended )
1143 char *retoid = NULL;
1144 struct berval *retdata = NULL;
1147 printf("# extended result response\n");
1150 rc = ldap_parse_extended_result( ld, extended,
1151 &retoid, &retdata, 0 );
1153 if( rc != LDAP_SUCCESS ) {
1154 ldap_perror(ld, "ldap_parse_extended_result");
1155 exit( EXIT_FAILURE );
1158 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1159 "extended", retoid, retoid ? strlen(retoid) : 0 );
1160 ber_memfree( retoid );
1163 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1164 "data", retdata->bv_val, retdata->bv_len );
1165 ber_bvfree( retdata );
1168 print_result( ld, extended, 0 );
1171 static void print_partial(
1173 LDAPMessage *partial )
1176 char *retoid = NULL;
1177 struct berval *retdata = NULL;
1178 LDAPControl **ctrls = NULL;
1181 printf("# extended partial response\n");
1184 rc = ldap_parse_extended_partial( ld, partial,
1185 &retoid, &retdata, &ctrls, 0 );
1187 if( rc != LDAP_SUCCESS ) {
1188 ldap_perror(ld, "ldap_parse_extended_partial");
1189 exit( EXIT_FAILURE );
1192 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1193 "partial", retoid, retoid ? strlen(retoid) : 0 );
1195 ber_memfree( retoid );
1198 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1200 retdata->bv_val, retdata->bv_len );
1202 ber_bvfree( retdata );
1206 print_ctrls( ctrls );
1207 ldap_controls_free( ctrls );
1211 static int print_result(
1213 LDAPMessage *result, int search )
1218 char *matcheddn = NULL;
1221 LDAPControl **ctrls = NULL;
1225 printf("# search result\n");
1228 printf("%s: %d\n", "search", ldap_msgid(result) );
1232 rc = ldap_parse_result( ld, result,
1233 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1235 if( rc != LDAP_SUCCESS ) {
1236 ldap_perror(ld, "ldap_parse_result");
1237 exit( EXIT_FAILURE );
1242 printf( "result: %d %s\n", err, ldap_err2string(err) );
1244 } else if ( err != LDAP_SUCCESS ) {
1245 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1248 if( matcheddn && *matcheddn ) {
1250 write_ldif( LDIF_PUT_VALUE,
1251 "matchedDN", matcheddn, strlen(matcheddn) );
1253 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1256 ber_memfree( matcheddn );
1259 if( text && *text ) {
1261 write_ldif( LDIF_PUT_TEXT, "text",
1262 text, strlen(text) );
1264 fprintf( stderr, "Additional information: %s\n", text );
1267 ber_memfree( text );
1272 for( i=0; refs[i] != NULL; i++ ) {
1274 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1276 fprintf( stderr, "Referral: %s\n", refs[i] );
1280 ber_memvfree( (void **) refs );
1284 print_ctrls( ctrls );
1285 ldap_controls_free( ctrls );
1291 void print_ctrls( LDAPControl **ctrls ) {
1293 for(i=0; ctrls[i] != NULL; i++ ) {
1294 /* control: OID criticality base64value */
1295 struct berval *b64 = NULL;
1299 len = strlen( ctrls[i]->ldctl_oid );
1301 /* add enough for space after OID and the critical value itself */
1302 len += ctrls[i]->ldctl_iscritical
1303 ? sizeof("true") : sizeof("false");
1305 /* convert to base64 */
1306 if( ctrls[i]->ldctl_value.bv_len ) {
1307 b64 = ber_memalloc( sizeof(struct berval) );
1309 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1310 ctrls[i]->ldctl_value.bv_len ) + 1;
1311 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1313 b64->bv_len = lutil_b64_ntop(
1314 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1315 b64->bv_val, b64->bv_len );
1319 len += 1 + b64->bv_len;
1322 str = malloc( len + 1 );
1323 strcpy( str, ctrls[i]->ldctl_oid );
1324 strcat( str, ctrls[i]->ldctl_iscritical
1325 ? " true" : " false" );
1329 strcat(str, b64->bv_val );
1332 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1333 "control", str, len );
1341 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1345 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1349 fputs( ldif, stdout );
1350 ber_memfree( ldif );