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 "\tfilter\tRFC-2254 compliant LDAP search filter\n"
43 "\tattributes\twhitespace-separated list of attribute descriptions\n"
44 "\t which may include:\n"
45 "\t\t1.1 -- no attributes\n"
46 "\t\t* -- all user attributes\n"
47 "\t\t+ -- all operational attributes\n"
50 "\t-a deref\tone of never (default), always, search, or find\n"
51 "\t-A\t\tretrieve attribute names only (no values)\n"
52 "\t-b basedn\tbase dn for search\n"
53 "\t-l limit\ttime limit (in seconds) for search\n"
54 "\t-L\t\tprint responses in LDIFv1 format\n"
55 "\t-LL\t\tprint responses in LDIF format without comments\n"
56 "\t-LLL\t\tprint responses in LDIF format without comments\n"
58 "\t-s scope\tone of base, one, or sub (search scope)\n"
59 "\t-S attr\t\tsort the results by attribute `attr'\n"
60 "\t-t\t\twrite binary values to files in temporary directory\n"
61 "\t-tt\t\twrite all values to files in temporary directory\n"
62 "\t-T path\t\twrite files to directory specified by path (default:\n"
63 "\t\t\t\"" LDAP_TMPDIR "\")\n"
64 "\t-u\t\tinclude User Friendly entry names in the output\n"
67 "\t-d level\tset LDAP debugging level to `level'\n"
68 "\t-D binddn\tbind DN\n"
69 "\t-f file\t\tread operations from `file'\n"
70 "\t-h host\t\tLDAP server\n"
71 "\t-I\t\tuse SASL Interactive mode\n"
72 "\t-k\t\tuse Kerberos authentication\n"
73 "\t-K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
74 "\t-M\t\tenable Manage DSA IT control (-MM to make critical)\n"
75 "\t-n\t\tshow what would be done but don't actually search\n"
76 "\t-O secprops\tSASL security properties\n"
77 "\t-p port\t\tport on LDAP server\n"
78 "\t-P version\tprocotol version (default: 3)\n"
79 "\t-Q\t\tuse SASL Quiet mode\n"
80 "\t-R realm\tSASL realm\n"
81 "\t-U user\t\tSASL authentication identity (username)\n"
82 "\t-v\t\trun in verbose mode (diagnostics to standard output)\n"
83 "\t-V prefix\tURL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
84 "\t-w passwd\tbind passwd (for simple authentication)\n"
85 "\t-W\t\tprompt for bind passwd\n"
86 "\t-x\t\tSimple authentication\n"
87 "\t-X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
88 "\t-Y mech\t\tSASL mechanism\n"
89 "\t-z limit\tsize limit (in entries) for search\n"
90 "\t-Z\t\tissue 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 *binddn = NULL;
144 static struct berval passwd = { 0, NULL };
145 static char *base = NULL;
146 static char *ldaphost = NULL;
147 static int ldapport = 0;
148 #ifdef HAVE_CYRUS_SASL
149 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
150 static char *sasl_realm = NULL;
151 static char *sasl_authc_id = NULL;
152 static char *sasl_authz_id = NULL;
153 static char *sasl_mech = NULL;
154 static char *sasl_secprops = NULL;
156 static int use_tls = 0;
157 static char *sortattr = NULL;
158 static int verbose, not, includeufn, vals2tmp, ldif;
161 main( int argc, char **argv )
163 char *prog, *infile, *filtpattern, **attrs, line[BUFSIZ];
165 int rc, i, first, scope, deref, attrsonly, manageDSAit;
166 int referrals, timelimit, sizelimit, debug;
167 int authmethod, version, want_bindpw;
171 debug = verbose = not = vals2tmp = referrals =
172 attrsonly = manageDSAit = ldif = want_bindpw = 0;
174 deref = sizelimit = timelimit = version = -1;
176 scope = LDAP_SCOPE_SUBTREE;
179 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : ++prog;
181 while (( i = getopt( argc, argv,
182 "Aa:b:f:Ll:S:s:T:tuV:z:" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z")) != EOF )
186 case 'a': /* set alias deref option */
187 if ( strcasecmp( optarg, "never" ) == 0 ) {
188 deref = LDAP_DEREF_NEVER;
189 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
190 deref = LDAP_DEREF_SEARCHING;
191 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
192 deref = LDAP_DEREF_FINDING;
193 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
194 deref = LDAP_DEREF_ALWAYS;
196 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
200 case 'A': /* retrieve attribute names only -- no values */
203 case 'b': /* search base */
204 base = strdup( optarg );
206 case 'f': /* input file */
207 if( infile != NULL ) {
208 fprintf( stderr, "%s: -f previously specified\n" );
211 infile = strdup( optarg );
213 case 'l': /* time limit */
214 timelimit = atoi( optarg );
216 case 'L': /* print entries in LDIF format */
219 case 's': /* search scope */
220 if ( strcasecmp( optarg, "base" ) == 0 ) {
221 scope = LDAP_SCOPE_BASE;
222 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
223 scope = LDAP_SCOPE_ONELEVEL;
224 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
225 scope = LDAP_SCOPE_SUBTREE;
227 fprintf( stderr, "scope should be base, one, or sub\n" );
231 case 'S': /* sort attribute */
232 sortattr = strdup( optarg );
234 case 'u': /* include UFN */
237 case 't': /* write attribute values to TMPDIR files */
240 case 'T': /* tmpdir */
241 if( tmpdir ) free( tmpdir );
242 tmpdir = strdup( optarg );
244 case 'V': /* uri prefix */
245 if( urlpre ) free( urlpre );
246 urlpre = strdup( optarg );
248 case 'z': /* size limit */
249 sizelimit = atoi( optarg );
257 debug |= atoi( optarg );
259 case 'D': /* bind DN */
260 if( binddn != NULL ) {
261 fprintf( stderr, "%s: -D previously specified\n" );
264 binddn = strdup( optarg );
266 case 'h': /* ldap host */
267 if( ldaphost != NULL ) {
268 fprintf( stderr, "%s: -h previously specified\n" );
271 ldaphost = strdup( optarg );
274 #ifdef HAVE_CYRUS_SASL
275 if( version == LDAP_VERSION2 ) {
276 fprintf( stderr, "%s: -I incompatible with version %d\n",
280 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
281 fprintf( stderr, "%s: incompatible previous "
282 "authentication choice\n",
286 authmethod = LDAP_AUTH_SASL;
287 version = LDAP_VERSION3;
288 sasl_flags = LDAP_SASL_INTERACTIVE;
291 fprintf( stderr, "%s: was not compiled with SASL support\n",
293 return( EXIT_FAILURE );
295 case 'k': /* kerberos bind */
296 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
297 if( version > LDAP_VERSION2 ) {
298 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
303 if( authmethod != -1 ) {
304 fprintf( stderr, "%s: -k incompatible with previous "
305 "authentication choice\n", prog );
309 authmethod = LDAP_AUTH_KRBV4;
311 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
315 case 'K': /* kerberos bind, part one only */
316 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
317 if( version > LDAP_VERSION2 ) {
318 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
322 if( authmethod != -1 ) {
323 fprintf( stderr, "%s: incompatible with previous "
324 "authentication choice\n", prog );
328 authmethod = LDAP_AUTH_KRBV41;
330 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
331 return( EXIT_FAILURE );
335 /* enable Manage DSA IT */
336 if( version == LDAP_VERSION2 ) {
337 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
342 version = LDAP_VERSION3;
344 case 'n': /* print deletes, don't actually do them */
348 #ifdef HAVE_CYRUS_SASL
349 if( sasl_secprops != NULL ) {
350 fprintf( stderr, "%s: -O previously specified\n" );
353 if( version == LDAP_VERSION2 ) {
354 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
358 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
359 fprintf( stderr, "%s: incompatible previous "
360 "authentication choice\n", prog );
363 authmethod = LDAP_AUTH_SASL;
364 version = LDAP_VERSION3;
365 sasl_secprops = strdup( optarg );
367 fprintf( stderr, "%s: not compiled with SASL support\n",
369 return( EXIT_FAILURE );
374 fprintf( stderr, "%s: -p previously specified\n" );
377 ldapport = atoi( optarg );
380 switch( atoi(optarg) ) {
382 if( version == LDAP_VERSION3 ) {
383 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
387 version = LDAP_VERSION2;
390 if( version == LDAP_VERSION2 ) {
391 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
395 version = LDAP_VERSION3;
398 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
401 return( EXIT_FAILURE );
404 #ifdef HAVE_CYRUS_SASL
405 if( version == LDAP_VERSION2 ) {
406 fprintf( stderr, "%s: -Q incompatible with version %d\n",
410 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
411 fprintf( stderr, "%s: incompatible previous "
412 "authentication choice\n",
416 authmethod = LDAP_AUTH_SASL;
417 version = LDAP_VERSION3;
418 sasl_flags = LDAP_SASL_QUIET;
421 fprintf( stderr, "%s: was not compiled with SASL support\n",
423 return( EXIT_FAILURE );
426 #ifdef HAVE_CYRUS_SASL
427 if( sasl_realm != NULL ) {
428 fprintf( stderr, "%s: -R previously specified\n" );
431 if( version == LDAP_VERSION2 ) {
432 fprintf( stderr, "%s: -R incompatible with version %d\n",
436 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
437 fprintf( stderr, "%s: incompatible previous "
438 "authentication choice\n",
442 authmethod = LDAP_AUTH_SASL;
443 version = LDAP_VERSION3;
444 sasl_realm = strdup( optarg );
446 fprintf( stderr, "%s: was not compiled with SASL support\n",
448 return( EXIT_FAILURE );
452 #ifdef HAVE_CYRUS_SASL
453 if( sasl_authc_id != NULL ) {
454 fprintf( stderr, "%s: -U previously specified\n" );
457 if( version == LDAP_VERSION2 ) {
458 fprintf( stderr, "%s: -U incompatible with version %d\n",
462 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
463 fprintf( stderr, "%s: incompatible previous "
464 "authentication choice\n",
468 authmethod = LDAP_AUTH_SASL;
469 version = LDAP_VERSION3;
470 sasl_authc_id = strdup( optarg );
472 fprintf( stderr, "%s: was not compiled with SASL support\n",
474 return( EXIT_FAILURE );
477 case 'v': /* verbose mode */
480 case 'w': /* password */
481 passwd.bv_val = strdup( optarg );
485 for( p = optarg; *p == '\0'; p++ ) {
489 passwd.bv_len = strlen( passwd.bv_val );
495 #ifdef HAVE_CYRUS_SASL
496 if( sasl_mech != NULL ) {
497 fprintf( stderr, "%s: -Y previously specified\n" );
500 if( version == LDAP_VERSION2 ) {
501 fprintf( stderr, "%s: -Y incompatible with version %d\n",
505 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
506 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
509 authmethod = LDAP_AUTH_SASL;
510 version = LDAP_VERSION3;
511 sasl_mech = strdup( optarg );
513 fprintf( stderr, "%s: was not compiled with SASL support\n",
515 return( EXIT_FAILURE );
519 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
520 fprintf( stderr, "%s: incompatible with previous "
521 "authentication choice\n", prog );
524 authmethod = LDAP_AUTH_SIMPLE;
527 #ifdef HAVE_CYRUS_SASL
528 if( sasl_authz_id != NULL ) {
529 fprintf( stderr, "%s: -X previously specified\n" );
532 if( version == LDAP_VERSION2 ) {
533 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
537 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
538 fprintf( stderr, "%s: -X incompatible with "
539 "authentication choice\n", prog );
542 authmethod = LDAP_AUTH_SASL;
543 version = LDAP_VERSION3;
544 sasl_authz_id = strdup( optarg );
546 fprintf( stderr, "%s: not compiled with SASL support\n",
548 return( EXIT_FAILURE );
553 if( version == LDAP_VERSION2 ) {
554 fprintf( stderr, "%s -Z incompatible with version %d\n",
558 version = LDAP_VERSION3;
561 fprintf( stderr, "%s: not compiled with TLS support\n",
563 return( EXIT_FAILURE );
567 fprintf( stderr, "%s: unrecongized option -%c\n",
574 version = LDAP_VERSION3;
576 if (authmethod == -1 && version > LDAP_VERSION2) {
577 #ifdef HAVE_CYRUS_SASL
578 authmethod = LDAP_AUTH_SASL;
580 authmethod = LDAP_AUTH_SIMPLE;
584 if (( argc - optind < 1 ) ||
585 ( strchr( argv[optind], '=' ) == NULL ) )
587 filtpattern = "(objectclass=*)";
589 filtpattern = strdup( argv[optind++] );
592 if ( argv[optind] == NULL ) {
594 } else if ( sortattr == NULL || *sortattr == '\0' ) {
595 attrs = &argv[optind];
598 if ( infile != NULL ) {
599 if ( infile[0] == '-' && infile[1] == '\0' ) {
601 } else if (( fp = fopen( infile, "r" )) == NULL ) {
608 && (tmpdir = getenv("TMPDIR")) == NULL
609 && (tmpdir = getenv("TMP")) == NULL
610 && (tmpdir = getenv("TEMP")) == NULL )
612 tmpdir = LDAP_TMPDIR;
615 if( urlpre == NULL ) {
616 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
618 if( urlpre == NULL ) {
623 sprintf( urlpre, "file:///%s/",
624 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
626 /* urlpre should be URLized.... */
630 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
631 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
633 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
634 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
640 (void) SIGNAL( SIGPIPE, SIG_IGN );
645 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
646 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
650 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
651 perror( "ldap_init" );
656 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
658 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
661 if (timelimit != -1 &&
662 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
664 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
667 if (sizelimit != -1 &&
668 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
670 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
675 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
676 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
678 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
679 referrals ? "on" : "off" );
683 if (version == -1 ) {
687 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
688 != LDAP_OPT_SUCCESS )
690 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
695 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
697 ldap_perror( ld, "ldap_start_tls" );
700 fprintf( stderr, "WARNING: could not start TLS\n" );
704 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
705 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
708 if ( authmethod == LDAP_AUTH_SASL ) {
709 #ifdef HAVE_CYRUS_SASL
712 if( sasl_secprops != NULL ) {
713 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
714 (void *) sasl_secprops );
716 if( rc != LDAP_OPT_SUCCESS ) {
718 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
720 return( EXIT_FAILURE );
724 defaults = lutil_sasl_defaults( ld,
731 rc = ldap_sasl_interactive_bind_s( ld, binddn,
732 sasl_mech, NULL, NULL,
733 sasl_flags, lutil_sasl_interact, defaults );
735 if( rc != LDAP_SUCCESS ) {
736 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
737 return( EXIT_FAILURE );
740 fprintf( stderr, "%s was not compiled with SASL support\n",
742 return( EXIT_FAILURE );
745 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
747 ldap_perror( ld, "ldap_bind" );
748 return( EXIT_FAILURE );
755 LDAPControl *ctrls[2];
759 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
760 c.ldctl_value.bv_val = NULL;
761 c.ldctl_value.bv_len = 0;
762 c.ldctl_iscritical = manageDSAit > 1;
764 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
766 if( err != LDAP_OPT_SUCCESS ) {
767 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
768 c.ldctl_iscritical ? "critical " : "" );
769 if( c.ldctl_iscritical ) {
770 exit( EXIT_FAILURE );
776 fprintf( stderr, "filter%s: %s\nrequesting: ",
777 infile != NULL ? " pattern" : "",
780 if ( attrs == NULL ) {
781 fprintf( stderr, "ALL" );
783 for ( i = 0; attrs[ i ] != NULL; ++i ) {
784 fprintf( stderr, "%s ", attrs[ i ] );
787 fprintf( stderr, "\n" );
791 printf( "version: %d\n\n", ldif ? 1 : 2 );
795 printf( "#\n# filter%s: %s\n# requesting: ",
796 infile != NULL ? " pattern" : "",
799 if ( attrs == NULL ) {
802 for ( i = 0; attrs[ i ] != NULL; ++i ) {
803 printf( "%s ", attrs[ i ] );
808 printf("\n# with manageDSAit %scontrol",
809 manageDSAit > 1 ? "critical " : "" );
815 if ( infile == NULL ) {
816 rc = dosearch( ld, base, scope, NULL, filtpattern,
817 attrs, attrsonly, NULL, NULL, NULL, -1 );
822 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
823 line[ strlen( line ) - 1 ] = '\0';
829 rc = dosearch( ld, base, scope, filtpattern, line,
830 attrs, attrsonly, NULL, NULL, NULL, -1 );
850 LDAPControl **sctrls,
851 LDAPControl **cctrls,
852 struct timeval *timelimit,
855 char filter[ BUFSIZ ];
862 LDAPMessage *res, *msg;
865 if( filtpatt != NULL ) {
866 sprintf( filter, filtpatt, value );
869 fprintf( stderr, "filter is: (%s)\n", filter );
873 printf( "#\n# filter: %s\n#\n", filter );
877 sprintf( filter, "%s", value );
884 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
885 sctrls, cctrls, timelimit, sizelimit, &msgid );
887 if( rc != LDAP_SUCCESS ) {
888 fprintf( stderr, "ldapsearch: ldap_search_ext: %s (%d)\n",
889 ldap_err2string( rc ), rc );
893 nresponses = nentries = nreferences = nextended = npartial = 0;
897 while ((rc = ldap_result( ld, LDAP_RES_ANY,
898 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
902 (void) ldap_sort_entries( ld, &res,
903 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
906 for ( msg = ldap_first_message( ld, res );
908 msg = ldap_next_message( ld, msg ) )
910 if( nresponses++ ) putchar('\n');
912 switch( ldap_msgtype( msg ) ) {
913 case LDAP_RES_SEARCH_ENTRY:
915 print_entry( ld, msg, attrsonly );
918 case LDAP_RES_SEARCH_REFERENCE:
920 print_reference( ld, msg );
923 case LDAP_RES_EXTENDED:
925 print_extended( ld, msg );
927 if( ldap_msgid( msg ) == 0 ) {
928 /* unsolicited extended operation */
933 case LDAP_RES_EXTENDED_PARTIAL:
935 print_partial( ld, msg );
938 case LDAP_RES_SEARCH_RESULT:
939 rc = print_result( ld, msg, 1 );
948 ldap_perror( ld, "ldap_result" );
954 printf( "\n# numResponses: %d\n", nresponses );
955 if( nentries ) printf( "# numEntries: %d\n", nentries );
956 if( nextended ) printf( "# numExtended: %d\n", nextended );
957 if( npartial ) printf( "# numPartial: %d\n", npartial );
958 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
971 char tmpfname[ 256 ];
974 BerElement *ber = NULL;
975 struct berval **bvals;
976 LDAPControl **ctrls = NULL;
979 dn = ldap_get_dn( ld, entry );
983 ufn = ldap_dn2ufn( dn );
984 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
986 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
988 rc = ldap_get_entry_controls( ld, entry, &ctrls );
990 if( rc != LDAP_SUCCESS ) {
991 fprintf(stderr, "print_entry: %d\n", rc );
992 ldap_perror( ld, "ldap_get_entry_controls" );
993 exit( EXIT_FAILURE );
997 print_ctrls( ctrls );
998 ldap_controls_free( ctrls );
1003 ufn = ldap_dn2ufn( dn );
1005 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1008 if( ufn != NULL ) ldap_memfree( ufn );
1011 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1012 a = ldap_next_attribute( ld, entry, ber ) )
1015 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1017 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1018 for ( i = 0; bvals[i] != NULL; i++ ) {
1019 if ( vals2tmp > 1 || ( vals2tmp
1020 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1023 /* write value to file */
1024 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1028 if ( mktemp( tmpfname ) == NULL ) {
1033 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1038 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1043 if ( fwrite( bvals[ i ]->bv_val,
1044 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1053 sprintf( url, "%s%s", urlpre,
1054 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1056 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1059 write_ldif( LDIF_PUT_VALUE, a,
1060 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1063 ber_bvecfree( bvals );
1072 static void print_reference(
1074 LDAPMessage *reference )
1078 LDAPControl **ctrls;
1081 printf("# search reference\n");
1084 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1086 if( rc != LDAP_SUCCESS ) {
1087 ldap_perror(ld, "ldap_parse_reference");
1088 exit( EXIT_FAILURE );
1093 for( i=0; refs[i] != NULL; i++ ) {
1094 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1095 "ref", refs[i], strlen(refs[i]) );
1097 ber_memvfree( (void **) refs );
1101 print_ctrls( ctrls );
1102 ldap_controls_free( ctrls );
1106 static void print_extended(
1108 LDAPMessage *extended )
1111 char *retoid = NULL;
1112 struct berval *retdata = NULL;
1115 printf("# extended result response\n");
1118 rc = ldap_parse_extended_result( ld, extended,
1119 &retoid, &retdata, 0 );
1121 if( rc != LDAP_SUCCESS ) {
1122 ldap_perror(ld, "ldap_parse_extended_result");
1123 exit( EXIT_FAILURE );
1126 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1127 "extended", retoid, retoid ? strlen(retoid) : 0 );
1128 ber_memfree( retoid );
1131 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1132 "data", retdata->bv_val, retdata->bv_len );
1133 ber_bvfree( retdata );
1136 print_result( ld, extended, 0 );
1139 static void print_partial(
1141 LDAPMessage *partial )
1144 char *retoid = NULL;
1145 struct berval *retdata = NULL;
1146 LDAPControl **ctrls = NULL;
1149 printf("# extended partial response\n");
1152 rc = ldap_parse_extended_partial( ld, partial,
1153 &retoid, &retdata, &ctrls, 0 );
1155 if( rc != LDAP_SUCCESS ) {
1156 ldap_perror(ld, "ldap_parse_extended_partial");
1157 exit( EXIT_FAILURE );
1160 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1161 "partial", retoid, retoid ? strlen(retoid) : 0 );
1163 ber_memfree( retoid );
1166 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1168 retdata->bv_val, retdata->bv_len );
1170 ber_bvfree( retdata );
1174 print_ctrls( ctrls );
1175 ldap_controls_free( ctrls );
1179 static int print_result(
1181 LDAPMessage *result, int search )
1186 char *matcheddn = NULL;
1189 LDAPControl **ctrls = NULL;
1193 printf("# search result\n");
1196 printf("%s: %d\n", "search", ldap_msgid(result) );
1200 rc = ldap_parse_result( ld, result,
1201 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1203 if( rc != LDAP_SUCCESS ) {
1204 ldap_perror(ld, "ldap_parse_result");
1205 exit( EXIT_FAILURE );
1210 printf( "result: %d %s\n", err, ldap_err2string(err) );
1212 } else if ( err != LDAP_SUCCESS ) {
1213 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1216 if( matcheddn && *matcheddn ) {
1218 write_ldif( LDIF_PUT_VALUE,
1219 "matchedDN", matcheddn, strlen(matcheddn) );
1221 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1224 ber_memfree( matcheddn );
1227 if( text && *text ) {
1229 write_ldif( LDIF_PUT_TEXT, "text",
1230 text, strlen(text) );
1232 fprintf( stderr, "Additional information: %s\n", text );
1235 ber_memfree( text );
1240 for( i=0; refs[i] != NULL; i++ ) {
1242 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1244 fprintf( stderr, "Referral: %s\n", refs[i] );
1248 ber_memvfree( (void **) refs );
1252 print_ctrls( ctrls );
1253 ldap_controls_free( ctrls );
1259 void print_ctrls( LDAPControl **ctrls ) {
1261 for(i=0; ctrls[i] != NULL; i++ ) {
1262 /* control: OID criticality base64value */
1263 struct berval *b64 = NULL;
1267 len = strlen( ctrls[i]->ldctl_oid );
1269 /* add enough for space after OID and the critical value itself */
1270 len += ctrls[i]->ldctl_iscritical
1271 ? sizeof("true") : sizeof("false");
1273 /* convert to base64 */
1274 if( ctrls[i]->ldctl_value.bv_len ) {
1275 b64 = ber_memalloc( sizeof(struct berval) );
1277 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1278 ctrls[i]->ldctl_value.bv_len ) + 1;
1279 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1281 b64->bv_len = lutil_b64_ntop(
1282 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1283 b64->bv_val, b64->bv_len );
1287 len += 1 + b64->bv_len;
1290 str = malloc( len + 1 );
1291 strcpy( str, ctrls[i]->ldctl_oid );
1292 strcat( str, ctrls[i]->ldctl_iscritical
1293 ? " true" : " false" );
1297 strcat(str, b64->bv_val );
1300 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1301 "control", str, len );
1309 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1313 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1317 fputs( ldif, stdout );
1318 ber_memfree( ldif );