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 int ldapport = 0;
149 #ifdef HAVE_CYRUS_SASL
150 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
151 static char *sasl_realm = NULL;
152 static char *sasl_authc_id = NULL;
153 static char *sasl_authz_id = NULL;
154 static char *sasl_mech = NULL;
155 static char *sasl_secprops = NULL;
157 static int use_tls = 0;
158 static char *sortattr = NULL;
159 static int verbose, not, includeufn, vals2tmp, ldif;
162 main( int argc, char **argv )
164 char *infile, *filtpattern, **attrs, line[BUFSIZ];
166 int rc, i, first, scope, deref, attrsonly, manageDSAit;
167 int referrals, timelimit, sizelimit, debug;
168 int authmethod, version, want_bindpw;
172 debug = verbose = not = vals2tmp = referrals =
173 attrsonly = manageDSAit = ldif = want_bindpw = 0;
175 deref = sizelimit = timelimit = version = -1;
177 scope = LDAP_SCOPE_SUBTREE;
180 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
182 while (( i = getopt( argc, argv,
183 "Aa:b:f:Ll:S:s:T:tuV:z:" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z")) != EOF )
187 case 'a': /* set alias deref option */
188 if ( strcasecmp( optarg, "never" ) == 0 ) {
189 deref = LDAP_DEREF_NEVER;
190 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
191 deref = LDAP_DEREF_SEARCHING;
192 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
193 deref = LDAP_DEREF_FINDING;
194 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
195 deref = LDAP_DEREF_ALWAYS;
197 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
201 case 'A': /* retrieve attribute names only -- no values */
204 case 'b': /* search base */
205 base = strdup( optarg );
207 case 'f': /* input file */
208 if( infile != NULL ) {
209 fprintf( stderr, "%s: -f previously specified\n" );
212 infile = strdup( optarg );
214 case 'l': /* time limit */
215 timelimit = atoi( optarg );
217 case 'L': /* print entries in LDIF format */
220 case 's': /* search scope */
221 if ( strcasecmp( optarg, "base" ) == 0 ) {
222 scope = LDAP_SCOPE_BASE;
223 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
224 scope = LDAP_SCOPE_ONELEVEL;
225 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
226 scope = LDAP_SCOPE_SUBTREE;
228 fprintf( stderr, "scope should be base, one, or sub\n" );
232 case 'S': /* sort attribute */
233 sortattr = strdup( optarg );
235 case 'u': /* include UFN */
238 case 't': /* write attribute values to TMPDIR files */
241 case 'T': /* tmpdir */
242 if( tmpdir ) free( tmpdir );
243 tmpdir = strdup( optarg );
245 case 'V': /* uri prefix */
246 if( urlpre ) free( urlpre );
247 urlpre = strdup( optarg );
249 case 'z': /* size limit */
250 sizelimit = atoi( optarg );
258 debug |= atoi( optarg );
260 case 'D': /* bind DN */
261 if( binddn != NULL ) {
262 fprintf( stderr, "%s: -D previously specified\n" );
265 binddn = strdup( optarg );
267 case 'h': /* ldap host */
268 if( ldaphost != NULL ) {
269 fprintf( stderr, "%s: -h previously specified\n" );
272 ldaphost = strdup( optarg );
275 #ifdef HAVE_CYRUS_SASL
276 if( version == LDAP_VERSION2 ) {
277 fprintf( stderr, "%s: -I incompatible with version %d\n",
281 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
282 fprintf( stderr, "%s: incompatible previous "
283 "authentication choice\n",
287 authmethod = LDAP_AUTH_SASL;
288 version = LDAP_VERSION3;
289 sasl_flags = LDAP_SASL_INTERACTIVE;
292 fprintf( stderr, "%s: was not compiled with SASL support\n",
294 return( EXIT_FAILURE );
296 case 'k': /* kerberos bind */
297 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
298 if( version > LDAP_VERSION2 ) {
299 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
304 if( authmethod != -1 ) {
305 fprintf( stderr, "%s: -k incompatible with previous "
306 "authentication choice\n", prog );
310 authmethod = LDAP_AUTH_KRBV4;
312 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
316 case 'K': /* kerberos bind, part one only */
317 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
318 if( version > LDAP_VERSION2 ) {
319 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
323 if( authmethod != -1 ) {
324 fprintf( stderr, "%s: incompatible with previous "
325 "authentication choice\n", prog );
329 authmethod = LDAP_AUTH_KRBV41;
331 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
332 return( EXIT_FAILURE );
336 /* enable Manage DSA IT */
337 if( version == LDAP_VERSION2 ) {
338 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
343 version = LDAP_VERSION3;
345 case 'n': /* print deletes, don't actually do them */
349 #ifdef HAVE_CYRUS_SASL
350 if( sasl_secprops != NULL ) {
351 fprintf( stderr, "%s: -O previously specified\n" );
354 if( version == LDAP_VERSION2 ) {
355 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
359 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
360 fprintf( stderr, "%s: incompatible previous "
361 "authentication choice\n", prog );
364 authmethod = LDAP_AUTH_SASL;
365 version = LDAP_VERSION3;
366 sasl_secprops = strdup( optarg );
368 fprintf( stderr, "%s: not compiled with SASL support\n",
370 return( EXIT_FAILURE );
375 fprintf( stderr, "%s: -p previously specified\n" );
378 ldapport = atoi( optarg );
381 switch( atoi(optarg) ) {
383 if( version == LDAP_VERSION3 ) {
384 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
388 version = LDAP_VERSION2;
391 if( version == LDAP_VERSION2 ) {
392 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
396 version = LDAP_VERSION3;
399 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
402 return( EXIT_FAILURE );
405 #ifdef HAVE_CYRUS_SASL
406 if( version == LDAP_VERSION2 ) {
407 fprintf( stderr, "%s: -Q incompatible with version %d\n",
411 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
412 fprintf( stderr, "%s: incompatible previous "
413 "authentication choice\n",
417 authmethod = LDAP_AUTH_SASL;
418 version = LDAP_VERSION3;
419 sasl_flags = LDAP_SASL_QUIET;
422 fprintf( stderr, "%s: not compiled with SASL support\n",
424 return( EXIT_FAILURE );
427 #ifdef HAVE_CYRUS_SASL
428 if( sasl_realm != NULL ) {
429 fprintf( stderr, "%s: -R previously specified\n" );
432 if( version == LDAP_VERSION2 ) {
433 fprintf( stderr, "%s: -R incompatible with version %d\n",
437 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
438 fprintf( stderr, "%s: incompatible previous "
439 "authentication choice\n",
443 authmethod = LDAP_AUTH_SASL;
444 version = LDAP_VERSION3;
445 sasl_realm = strdup( optarg );
447 fprintf( stderr, "%s: not compiled with SASL support\n",
449 return( EXIT_FAILURE );
453 #ifdef HAVE_CYRUS_SASL
454 if( sasl_authc_id != NULL ) {
455 fprintf( stderr, "%s: -U previously specified\n" );
458 if( version == LDAP_VERSION2 ) {
459 fprintf( stderr, "%s: -U incompatible with version %d\n",
463 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
464 fprintf( stderr, "%s: incompatible previous "
465 "authentication choice\n",
469 authmethod = LDAP_AUTH_SASL;
470 version = LDAP_VERSION3;
471 sasl_authc_id = strdup( optarg );
473 fprintf( stderr, "%s: not compiled with SASL support\n",
475 return( EXIT_FAILURE );
478 case 'v': /* verbose mode */
481 case 'w': /* password */
482 passwd.bv_val = strdup( optarg );
486 for( p = optarg; *p == '\0'; p++ ) {
490 passwd.bv_len = strlen( passwd.bv_val );
496 #ifdef HAVE_CYRUS_SASL
497 if( sasl_mech != NULL ) {
498 fprintf( stderr, "%s: -Y previously specified\n" );
501 if( version == LDAP_VERSION2 ) {
502 fprintf( stderr, "%s: -Y incompatible with version %d\n",
506 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
507 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
510 authmethod = LDAP_AUTH_SASL;
511 version = LDAP_VERSION3;
512 sasl_mech = strdup( optarg );
514 fprintf( stderr, "%s: not compiled with SASL support\n",
516 return( EXIT_FAILURE );
520 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
521 fprintf( stderr, "%s: incompatible with previous "
522 "authentication choice\n", prog );
525 authmethod = LDAP_AUTH_SIMPLE;
528 #ifdef HAVE_CYRUS_SASL
529 if( sasl_authz_id != NULL ) {
530 fprintf( stderr, "%s: -X previously specified\n" );
533 if( version == LDAP_VERSION2 ) {
534 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
538 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
539 fprintf( stderr, "%s: -X incompatible with "
540 "authentication choice\n", prog );
543 authmethod = LDAP_AUTH_SASL;
544 version = LDAP_VERSION3;
545 sasl_authz_id = strdup( optarg );
547 fprintf( stderr, "%s: not compiled with SASL support\n",
549 return( EXIT_FAILURE );
554 if( version == LDAP_VERSION2 ) {
555 fprintf( stderr, "%s: -Z incompatible with version %d\n",
559 version = LDAP_VERSION3;
562 fprintf( stderr, "%s: not compiled with TLS support\n",
564 return( EXIT_FAILURE );
568 fprintf( stderr, "%s: unrecongized option -%c\n",
575 version = LDAP_VERSION3;
577 if (authmethod == -1 && version > LDAP_VERSION2) {
578 #ifdef HAVE_CYRUS_SASL
579 authmethod = LDAP_AUTH_SASL;
581 authmethod = LDAP_AUTH_SIMPLE;
585 if (( argc - optind < 1 ) ||
586 ( *argv[optind] != '(' /*')'*/ &&
587 ( strchr( argv[optind], '=' ) == NULL ) ) )
589 filtpattern = "(objectclass=*)";
591 filtpattern = strdup( argv[optind++] );
594 if ( argv[optind] == NULL ) {
596 } else if ( sortattr == NULL || *sortattr == '\0' ) {
597 attrs = &argv[optind];
600 if ( infile != NULL ) {
601 if ( infile[0] == '-' && infile[1] == '\0' ) {
603 } else if (( fp = fopen( infile, "r" )) == NULL ) {
610 && (tmpdir = getenv("TMPDIR")) == NULL
611 && (tmpdir = getenv("TMP")) == NULL
612 && (tmpdir = getenv("TEMP")) == NULL )
614 tmpdir = LDAP_TMPDIR;
617 if( urlpre == NULL ) {
618 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
620 if( urlpre == NULL ) {
625 sprintf( urlpre, "file:///%s/",
626 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
628 /* urlpre should be URLized.... */
632 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
633 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
635 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
636 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
642 (void) SIGNAL( SIGPIPE, SIG_IGN );
647 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
648 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
652 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
653 perror( "ldap_init" );
658 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
660 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
663 if (timelimit != -1 &&
664 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
666 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
669 if (sizelimit != -1 &&
670 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
672 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
677 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
678 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
680 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
681 referrals ? "on" : "off" );
685 if (version == -1 ) {
689 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
690 != LDAP_OPT_SUCCESS )
692 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
697 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
699 ldap_perror( ld, "ldap_start_tls" );
702 fprintf( stderr, "WARNING: could not start TLS\n" );
706 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
707 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
710 if ( authmethod == LDAP_AUTH_SASL ) {
711 #ifdef HAVE_CYRUS_SASL
714 if( sasl_secprops != NULL ) {
715 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
716 (void *) sasl_secprops );
718 if( rc != LDAP_OPT_SUCCESS ) {
720 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
722 return( EXIT_FAILURE );
726 defaults = lutil_sasl_defaults( ld,
733 rc = ldap_sasl_interactive_bind_s( ld, binddn,
734 sasl_mech, NULL, NULL,
735 sasl_flags, lutil_sasl_interact, defaults );
737 if( rc != LDAP_SUCCESS ) {
738 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
739 return( EXIT_FAILURE );
742 fprintf( stderr, "%s: not compiled with SASL support\n",
744 return( EXIT_FAILURE );
747 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
749 ldap_perror( ld, "ldap_bind" );
750 return( EXIT_FAILURE );
757 LDAPControl *ctrls[2];
761 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
762 c.ldctl_value.bv_val = NULL;
763 c.ldctl_value.bv_len = 0;
764 c.ldctl_iscritical = manageDSAit > 1;
766 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
768 if( err != LDAP_OPT_SUCCESS ) {
769 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
770 c.ldctl_iscritical ? "critical " : "" );
771 if( c.ldctl_iscritical ) {
772 exit( EXIT_FAILURE );
778 fprintf( stderr, "filter%s: %s\nrequesting: ",
779 infile != NULL ? " pattern" : "",
782 if ( attrs == NULL ) {
783 fprintf( stderr, "ALL" );
785 for ( i = 0; attrs[ i ] != NULL; ++i ) {
786 fprintf( stderr, "%s ", attrs[ i ] );
789 fprintf( stderr, "\n" );
793 printf( "version: %d\n\n", ldif ? 1 : 2 );
797 printf( "#\n# filter%s: %s\n# requesting: ",
798 infile != NULL ? " pattern" : "",
801 if ( attrs == NULL ) {
804 for ( i = 0; attrs[ i ] != NULL; ++i ) {
805 printf( "%s ", attrs[ i ] );
810 printf("\n# with manageDSAit %scontrol",
811 manageDSAit > 1 ? "critical " : "" );
817 if ( infile == NULL ) {
818 rc = dosearch( ld, base, scope, NULL, filtpattern,
819 attrs, attrsonly, NULL, NULL, NULL, -1 );
824 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
825 line[ strlen( line ) - 1 ] = '\0';
831 rc = dosearch( ld, base, scope, filtpattern, line,
832 attrs, attrsonly, NULL, NULL, NULL, -1 );
852 LDAPControl **sctrls,
853 LDAPControl **cctrls,
854 struct timeval *timelimit,
857 char filter[ BUFSIZ ];
864 LDAPMessage *res, *msg;
867 if( filtpatt != NULL ) {
868 sprintf( filter, filtpatt, value );
871 fprintf( stderr, "filter is: (%s)\n", filter );
875 printf( "#\n# filter: %s\n#\n", filter );
879 sprintf( filter, "%s", value );
886 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
887 sctrls, cctrls, timelimit, sizelimit, &msgid );
889 if( rc != LDAP_SUCCESS ) {
890 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
891 prog, ldap_err2string( rc ), rc );
895 nresponses = nentries = nreferences = nextended = npartial = 0;
899 while ((rc = ldap_result( ld, LDAP_RES_ANY,
900 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
904 (void) ldap_sort_entries( ld, &res,
905 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
908 for ( msg = ldap_first_message( ld, res );
910 msg = ldap_next_message( ld, msg ) )
912 if( nresponses++ ) putchar('\n');
914 switch( ldap_msgtype( msg ) ) {
915 case LDAP_RES_SEARCH_ENTRY:
917 print_entry( ld, msg, attrsonly );
920 case LDAP_RES_SEARCH_REFERENCE:
922 print_reference( ld, msg );
925 case LDAP_RES_EXTENDED:
927 print_extended( ld, msg );
929 if( ldap_msgid( msg ) == 0 ) {
930 /* unsolicited extended operation */
935 case LDAP_RES_EXTENDED_PARTIAL:
937 print_partial( ld, msg );
940 case LDAP_RES_SEARCH_RESULT:
941 rc = print_result( ld, msg, 1 );
950 ldap_perror( ld, "ldap_result" );
956 printf( "\n# numResponses: %d\n", nresponses );
957 if( nentries ) printf( "# numEntries: %d\n", nentries );
958 if( nextended ) printf( "# numExtended: %d\n", nextended );
959 if( npartial ) printf( "# numPartial: %d\n", npartial );
960 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
973 char tmpfname[ 256 ];
976 BerElement *ber = NULL;
977 struct berval **bvals;
978 LDAPControl **ctrls = NULL;
981 dn = ldap_get_dn( ld, entry );
985 ufn = ldap_dn2ufn( dn );
986 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
988 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
990 rc = ldap_get_entry_controls( ld, entry, &ctrls );
992 if( rc != LDAP_SUCCESS ) {
993 fprintf(stderr, "print_entry: %d\n", rc );
994 ldap_perror( ld, "ldap_get_entry_controls" );
995 exit( EXIT_FAILURE );
999 print_ctrls( ctrls );
1000 ldap_controls_free( ctrls );
1005 ufn = ldap_dn2ufn( dn );
1007 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1010 if( ufn != NULL ) ldap_memfree( ufn );
1013 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1014 a = ldap_next_attribute( ld, entry, ber ) )
1017 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1019 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1020 for ( i = 0; bvals[i] != NULL; i++ ) {
1021 if ( vals2tmp > 1 || ( vals2tmp
1022 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1025 /* write value to file */
1026 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1030 if ( mktemp( tmpfname ) == NULL ) {
1035 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1040 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1045 if ( fwrite( bvals[ i ]->bv_val,
1046 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1055 sprintf( url, "%s%s", urlpre,
1056 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1058 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1061 write_ldif( LDIF_PUT_VALUE, a,
1062 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1065 ber_bvecfree( bvals );
1074 static void print_reference(
1076 LDAPMessage *reference )
1080 LDAPControl **ctrls;
1083 printf("# search reference\n");
1086 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1088 if( rc != LDAP_SUCCESS ) {
1089 ldap_perror(ld, "ldap_parse_reference");
1090 exit( EXIT_FAILURE );
1095 for( i=0; refs[i] != NULL; i++ ) {
1096 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1097 "ref", refs[i], strlen(refs[i]) );
1099 ber_memvfree( (void **) refs );
1103 print_ctrls( ctrls );
1104 ldap_controls_free( ctrls );
1108 static void print_extended(
1110 LDAPMessage *extended )
1113 char *retoid = NULL;
1114 struct berval *retdata = NULL;
1117 printf("# extended result response\n");
1120 rc = ldap_parse_extended_result( ld, extended,
1121 &retoid, &retdata, 0 );
1123 if( rc != LDAP_SUCCESS ) {
1124 ldap_perror(ld, "ldap_parse_extended_result");
1125 exit( EXIT_FAILURE );
1128 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1129 "extended", retoid, retoid ? strlen(retoid) : 0 );
1130 ber_memfree( retoid );
1133 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1134 "data", retdata->bv_val, retdata->bv_len );
1135 ber_bvfree( retdata );
1138 print_result( ld, extended, 0 );
1141 static void print_partial(
1143 LDAPMessage *partial )
1146 char *retoid = NULL;
1147 struct berval *retdata = NULL;
1148 LDAPControl **ctrls = NULL;
1151 printf("# extended partial response\n");
1154 rc = ldap_parse_extended_partial( ld, partial,
1155 &retoid, &retdata, &ctrls, 0 );
1157 if( rc != LDAP_SUCCESS ) {
1158 ldap_perror(ld, "ldap_parse_extended_partial");
1159 exit( EXIT_FAILURE );
1162 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1163 "partial", retoid, retoid ? strlen(retoid) : 0 );
1165 ber_memfree( retoid );
1168 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1170 retdata->bv_val, retdata->bv_len );
1172 ber_bvfree( retdata );
1176 print_ctrls( ctrls );
1177 ldap_controls_free( ctrls );
1181 static int print_result(
1183 LDAPMessage *result, int search )
1188 char *matcheddn = NULL;
1191 LDAPControl **ctrls = NULL;
1195 printf("# search result\n");
1198 printf("%s: %d\n", "search", ldap_msgid(result) );
1202 rc = ldap_parse_result( ld, result,
1203 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1205 if( rc != LDAP_SUCCESS ) {
1206 ldap_perror(ld, "ldap_parse_result");
1207 exit( EXIT_FAILURE );
1212 printf( "result: %d %s\n", err, ldap_err2string(err) );
1214 } else if ( err != LDAP_SUCCESS ) {
1215 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1218 if( matcheddn && *matcheddn ) {
1220 write_ldif( LDIF_PUT_VALUE,
1221 "matchedDN", matcheddn, strlen(matcheddn) );
1223 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1226 ber_memfree( matcheddn );
1229 if( text && *text ) {
1231 write_ldif( LDIF_PUT_TEXT, "text",
1232 text, strlen(text) );
1234 fprintf( stderr, "Additional information: %s\n", text );
1237 ber_memfree( text );
1242 for( i=0; refs[i] != NULL; i++ ) {
1244 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1246 fprintf( stderr, "Referral: %s\n", refs[i] );
1250 ber_memvfree( (void **) refs );
1254 print_ctrls( ctrls );
1255 ldap_controls_free( ctrls );
1261 void print_ctrls( LDAPControl **ctrls ) {
1263 for(i=0; ctrls[i] != NULL; i++ ) {
1264 /* control: OID criticality base64value */
1265 struct berval *b64 = NULL;
1269 len = strlen( ctrls[i]->ldctl_oid );
1271 /* add enough for space after OID and the critical value itself */
1272 len += ctrls[i]->ldctl_iscritical
1273 ? sizeof("true") : sizeof("false");
1275 /* convert to base64 */
1276 if( ctrls[i]->ldctl_value.bv_len ) {
1277 b64 = ber_memalloc( sizeof(struct berval) );
1279 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1280 ctrls[i]->ldctl_value.bv_len ) + 1;
1281 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1283 b64->bv_len = lutil_b64_ntop(
1284 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1285 b64->bv_val, b64->bv_len );
1289 len += 1 + b64->bv_len;
1292 str = malloc( len + 1 );
1293 strcpy( str, ctrls[i]->ldctl_oid );
1294 strcat( str, ctrls[i]->ldctl_iscritical
1295 ? " true" : " false" );
1299 strcat(str, b64->bv_val );
1302 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1303 "control", str, len );
1311 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1315 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1319 fputs( ldif, stdout );
1320 ber_memfree( ldif );