3 * Copyright 1998-2002 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 static char *def_tmpdir;
39 static char *def_urlpre;
42 usage( const char *s )
45 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
46 " filter\tRFC-2254 compliant LDAP search filter\n"
47 " attributes\twhitespace-separated list of attribute descriptions\n"
48 " which may include:\n"
49 " 1.1 no attributes\n"
50 " * all user attributes\n"
51 " + all operational attributes\n"
54 " -a deref one of never (default), always, search, or find\n"
55 " -A retrieve attribute names only (no values)\n"
56 " -b basedn base dn for search\n"
57 " -F prefix URL prefix for files (default: %s)\n"
58 " -l limit time limit (in seconds) for search\n"
59 " -L print responses in LDIFv1 format\n"
60 " -LL print responses in LDIF format without comments\n"
61 " -LLL print responses in LDIF format without comments\n"
63 " -s scope one of base, one, or sub (search scope)\n"
64 " -S attr sort the results by attribute `attr'\n"
65 " -t write binary values to files in temporary directory\n"
66 " -tt write all values to files in temporary directory\n"
67 " -T path write files to directory specified by path (default: %s)\n"
68 " -u include User Friendly entry names in the output\n"
69 " -z limit size limit (in entries) for search\n"
72 " -d level set LDAP debugging level to `level'\n"
73 " -D binddn bind DN\n"
74 " -f file read operations from `file'\n"
75 " -h host LDAP server\n"
76 " -H URI LDAP Uniform Resource Indentifier(s)\n"
77 " -I use SASL Interactive mode\n"
78 " -k use Kerberos authentication\n"
79 " -K like -k, but do only step 1 of the Kerberos bind\n"
80 " -M enable Manage DSA IT control (-MM to make critical)\n"
81 " -n show what would be done but don't actually search\n"
82 " -O props SASL security properties\n"
83 " -p port port on LDAP server\n"
84 " -P version procotol version (default: 3)\n"
85 " -Q use SASL Quiet mode\n"
86 " -R realm SASL realm\n"
87 " -U authcid SASL authentication identity\n"
88 " -v run in verbose mode (diagnostics to standard output)\n"
89 " -w passwd bind passwd (for simple authentication)\n"
90 " -W prompt for bind passwd\n"
91 " -x Simple authentication\n"
92 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
93 " -y file Read passwd from file\n"
94 " -Y mech SASL mechanism\n"
95 " -Z Start TLS request (-ZZ to require successful response)\n"
96 , s, def_urlpre, def_tmpdir );
101 static void print_entry LDAP_P((
106 static void print_reference(
108 LDAPMessage *reference );
110 static void print_extended(
112 LDAPMessage *extended );
114 static void print_partial(
116 LDAPMessage *partial );
118 static int print_result(
123 static void print_ctrls(
124 LDAPControl **ctrls );
126 static int write_ldif LDAP_P((
132 static int dosearch LDAP_P((
140 LDAPControl **sctrls,
141 LDAPControl **cctrls,
142 struct timeval *timeout,
145 static char *tmpdir = NULL;
146 static char *urlpre = NULL;
147 static char *prog = NULL;
148 static char *binddn = NULL;
149 static struct berval passwd = { 0, NULL };
150 static char *base = NULL;
151 static char *ldaphost = NULL;
152 static char *ldapuri = NULL;
153 static int ldapport = 0;
154 #ifdef HAVE_CYRUS_SASL
155 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
156 static char *sasl_realm = NULL;
157 static char *sasl_authc_id = NULL;
158 static char *sasl_authz_id = NULL;
159 static char *sasl_mech = NULL;
160 static char *sasl_secprops = NULL;
162 static int use_tls = 0;
163 static char *sortattr = NULL;
164 static int verbose, not, includeufn, vals2tmp, ldif;
171 if (*LDAP_DIRSEP != '/') {
172 for (p = url; *p; p++) {
173 if (*p == *LDAP_DIRSEP)
180 main( int argc, char **argv )
182 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
184 int rc, i, first, scope, deref, attrsonly, manageDSAit;
185 int referrals, timelimit, sizelimit, debug;
186 int authmethod, version, want_bindpw;
188 int valuesReturnFilter;
189 BerElement *ber = NULL;
190 struct berval *bvalp = NULL;
191 char *vrFilter = NULL, *control = NULL, *s;
192 char *pw_file = NULL;
196 debug = verbose = not = vals2tmp = referrals = valuesReturnFilter =
197 attrsonly = manageDSAit = ldif = want_bindpw = 0;
199 prog = lutil_progname( "ldapsearch", argc, argv );
201 lutil_log_initialize(argc, argv);
203 deref = sizelimit = timelimit = version = -1;
205 scope = LDAP_SCOPE_SUBTREE;
208 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
209 (def_tmpdir = getenv("TMP")) == NULL &&
210 (def_tmpdir = getenv("TEMP")) == NULL )
212 def_tmpdir = LDAP_TMPDIR;
216 def_tmpdir = LDAP_TMPDIR;
218 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
220 if( def_urlpre == NULL ) {
225 sprintf( def_urlpre, "file:///%s/",
226 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
228 urlize( def_urlpre );
230 while (( i = getopt( argc, argv, "Aa:b:E:F:f:Ll:S:s:T:tuz:"
231 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z")) != EOF )
235 case 'a': /* set alias deref option */
236 if ( strcasecmp( optarg, "never" ) == 0 ) {
237 deref = LDAP_DEREF_NEVER;
238 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
239 deref = LDAP_DEREF_SEARCHING;
240 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
241 deref = LDAP_DEREF_FINDING;
242 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
243 deref = LDAP_DEREF_ALWAYS;
245 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
249 case 'A': /* retrieve attribute names only -- no values */
252 case 'b': /* search base */
253 base = strdup( optarg );
255 case 'f': /* input file */
256 if( infile != NULL ) {
257 fprintf( stderr, "%s: -f previously specified\n", prog );
260 infile = strdup( optarg );
262 case 'E': /* controls */
263 if( version == LDAP_VERSION2 ) {
264 fprintf( stderr, "%s: -C incompatible with LDAPv%d\n",
269 /* should be extended to support comma separated list of
270 * key/value pairs: -E foo=123,bar=567
273 control = strdup( optarg );
274 if ( (s = strchr( control, '=' )) == NULL ) {
279 if ( strcasecmp( control, "mv" ) == 0 ) {
280 /* ValuesReturnFilter control */
281 if (valuesReturnFilter!=0) {
282 fprintf( stderr, "ValuesReturnFilter previously specified");
288 valuesReturnFilter=2;
290 valuesReturnFilter=1;
294 version = LDAP_VERSION3;
298 fprintf( stderr, "Invalid control name: %s\n", control );
303 case 'F': /* uri prefix */
304 if( urlpre ) free( urlpre );
305 urlpre = strdup( optarg );
307 case 'l': /* time limit */
308 timelimit = atoi( optarg );
309 if( timelimit < 0 ) {
310 fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
315 case 'L': /* print entries in LDIF format */
318 case 's': /* search scope */
319 if ( strcasecmp( optarg, "base" ) == 0 ) {
320 scope = LDAP_SCOPE_BASE;
321 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
322 scope = LDAP_SCOPE_ONELEVEL;
323 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
324 scope = LDAP_SCOPE_SUBTREE;
326 fprintf( stderr, "scope should be base, one, or sub\n" );
330 case 'S': /* sort attribute */
331 sortattr = strdup( optarg );
333 case 'u': /* include UFN */
336 case 't': /* write attribute values to TMPDIR files */
339 case 'T': /* tmpdir */
340 if( tmpdir ) free( tmpdir );
341 tmpdir = strdup( optarg );
343 case 'z': /* size limit */
344 sizelimit = atoi( optarg );
352 debug |= atoi( optarg );
354 case 'D': /* bind DN */
355 if( binddn != NULL ) {
356 fprintf( stderr, "%s: -D previously specified\n", prog );
359 binddn = strdup( optarg );
361 case 'h': /* ldap host */
362 if( ldapuri != NULL ) {
363 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
366 if( ldaphost != NULL ) {
367 fprintf( stderr, "%s: -h previously specified\n", prog );
370 ldaphost = strdup( optarg );
372 case 'H': /* ldap URI */
373 if( ldaphost != NULL ) {
374 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
378 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
381 if( ldapuri != NULL ) {
382 fprintf( stderr, "%s: -H previously specified\n", prog );
385 ldapuri = strdup( optarg );
388 #ifdef HAVE_CYRUS_SASL
389 if( version == LDAP_VERSION2 ) {
390 fprintf( stderr, "%s: -I incompatible with version %d\n",
394 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
395 fprintf( stderr, "%s: incompatible previous "
396 "authentication choice\n",
400 authmethod = LDAP_AUTH_SASL;
401 version = LDAP_VERSION3;
402 sasl_flags = LDAP_SASL_INTERACTIVE;
405 fprintf( stderr, "%s: was not compiled with SASL support\n",
407 return( EXIT_FAILURE );
409 case 'k': /* kerberos bind */
410 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
411 if( version > LDAP_VERSION2 ) {
412 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
417 if( authmethod != -1 ) {
418 fprintf( stderr, "%s: -k incompatible with previous "
419 "authentication choice\n", prog );
423 authmethod = LDAP_AUTH_KRBV4;
425 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
429 case 'K': /* kerberos bind, part one only */
430 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
431 if( version > LDAP_VERSION2 ) {
432 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
436 if( authmethod != -1 ) {
437 fprintf( stderr, "%s: incompatible with previous "
438 "authentication choice\n", prog );
442 authmethod = LDAP_AUTH_KRBV41;
444 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
445 return( EXIT_FAILURE );
449 /* enable Manage DSA IT */
450 if( version == LDAP_VERSION2 ) {
451 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
456 version = LDAP_VERSION3;
458 case 'n': /* print deletes, don't actually do them */
462 #ifdef HAVE_CYRUS_SASL
463 if( sasl_secprops != NULL ) {
464 fprintf( stderr, "%s: -O previously specified\n", prog );
467 if( version == LDAP_VERSION2 ) {
468 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
472 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
473 fprintf( stderr, "%s: incompatible previous "
474 "authentication choice\n", prog );
477 authmethod = LDAP_AUTH_SASL;
478 version = LDAP_VERSION3;
479 sasl_secprops = strdup( optarg );
481 fprintf( stderr, "%s: not compiled with SASL support\n",
483 return( EXIT_FAILURE );
488 fprintf( stderr, "%s: -p previously specified\n", prog );
491 ldapport = atoi( optarg );
494 switch( atoi(optarg) ) {
496 if( version == LDAP_VERSION3 ) {
497 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
501 version = LDAP_VERSION2;
504 if( version == LDAP_VERSION2 ) {
505 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
509 version = LDAP_VERSION3;
512 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
515 return( EXIT_FAILURE );
518 #ifdef HAVE_CYRUS_SASL
519 if( version == LDAP_VERSION2 ) {
520 fprintf( stderr, "%s: -Q incompatible with version %d\n",
524 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
525 fprintf( stderr, "%s: incompatible previous "
526 "authentication choice\n",
530 authmethod = LDAP_AUTH_SASL;
531 version = LDAP_VERSION3;
532 sasl_flags = LDAP_SASL_QUIET;
535 fprintf( stderr, "%s: not compiled with SASL support\n",
537 return( EXIT_FAILURE );
540 #ifdef HAVE_CYRUS_SASL
541 if( sasl_realm != NULL ) {
542 fprintf( stderr, "%s: -R previously specified\n", prog );
545 if( version == LDAP_VERSION2 ) {
546 fprintf( stderr, "%s: -R incompatible with version %d\n",
550 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
551 fprintf( stderr, "%s: incompatible previous "
552 "authentication choice\n",
556 authmethod = LDAP_AUTH_SASL;
557 version = LDAP_VERSION3;
558 sasl_realm = strdup( optarg );
560 fprintf( stderr, "%s: not compiled with SASL support\n",
562 return( EXIT_FAILURE );
566 #ifdef HAVE_CYRUS_SASL
567 if( sasl_authc_id != NULL ) {
568 fprintf( stderr, "%s: -U previously specified\n", prog );
571 if( version == LDAP_VERSION2 ) {
572 fprintf( stderr, "%s: -U incompatible with version %d\n",
576 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
577 fprintf( stderr, "%s: incompatible previous "
578 "authentication choice\n",
582 authmethod = LDAP_AUTH_SASL;
583 version = LDAP_VERSION3;
584 sasl_authc_id = strdup( optarg );
586 fprintf( stderr, "%s: not compiled with SASL support\n",
588 return( EXIT_FAILURE );
591 case 'v': /* verbose mode */
594 case 'w': /* password */
595 passwd.bv_val = strdup( optarg );
599 for( p = optarg; *p != '\0'; p++ ) {
603 passwd.bv_len = strlen( passwd.bv_val );
612 #ifdef HAVE_CYRUS_SASL
613 if( sasl_mech != NULL ) {
614 fprintf( stderr, "%s: -Y previously specified\n", prog );
617 if( version == LDAP_VERSION2 ) {
618 fprintf( stderr, "%s: -Y incompatible with version %d\n",
622 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
623 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
626 authmethod = LDAP_AUTH_SASL;
627 version = LDAP_VERSION3;
628 sasl_mech = strdup( optarg );
630 fprintf( stderr, "%s: not compiled with SASL support\n",
632 return( EXIT_FAILURE );
636 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
637 fprintf( stderr, "%s: incompatible with previous "
638 "authentication choice\n", prog );
641 authmethod = LDAP_AUTH_SIMPLE;
644 #ifdef HAVE_CYRUS_SASL
645 if( sasl_authz_id != NULL ) {
646 fprintf( stderr, "%s: -X previously specified\n", prog );
649 if( version == LDAP_VERSION2 ) {
650 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
654 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
655 fprintf( stderr, "%s: -X incompatible with "
656 "authentication choice\n", prog );
659 authmethod = LDAP_AUTH_SASL;
660 version = LDAP_VERSION3;
661 sasl_authz_id = strdup( optarg );
663 fprintf( stderr, "%s: not compiled with SASL support\n",
665 return( EXIT_FAILURE );
670 if( version == LDAP_VERSION2 ) {
671 fprintf( stderr, "%s: -Z incompatible with version %d\n",
675 version = LDAP_VERSION3;
678 fprintf( stderr, "%s: not compiled with TLS support\n",
680 return( EXIT_FAILURE );
684 fprintf( stderr, "%s: unrecognized option -%c\n",
691 version = LDAP_VERSION3;
693 if (authmethod == -1 && version > LDAP_VERSION2) {
694 #ifdef HAVE_CYRUS_SASL
695 authmethod = LDAP_AUTH_SASL;
697 authmethod = LDAP_AUTH_SIMPLE;
701 if (( argc - optind < 1 ) ||
702 ( *argv[optind] != '(' /*')'*/ &&
703 ( strchr( argv[optind], '=' ) == NULL ) ) )
705 filtpattern = "(objectclass=*)";
707 filtpattern = strdup( argv[optind++] );
710 if ( argv[optind] != NULL ) {
711 attrs = &argv[optind];
714 if ( infile != NULL ) {
715 if ( infile[0] == '-' && infile[1] == '\0' ) {
717 } else if (( fp = fopen( infile, "r" )) == NULL ) {
723 if ( tmpdir == NULL ) {
726 if ( urlpre == NULL )
730 if( urlpre == NULL ) {
731 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
733 if( urlpre == NULL ) {
738 sprintf( urlpre, "file:///%s/",
739 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
745 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
746 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
748 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
749 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
755 (void) SIGNAL( SIGPIPE, SIG_IGN );
758 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
760 fprintf( stderr, "ldap_init( %s, %d )\n",
761 ldaphost != NULL ? ldaphost : "<DEFAULT>",
765 ld = ldap_init( ldaphost, ldapport );
767 perror("ldapsearch: ldap_init");
773 fprintf( stderr, "ldap_initialize( %s )\n",
774 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
777 rc = ldap_initialize( &ld, ldapuri );
778 if( rc != LDAP_SUCCESS ) {
779 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
780 rc, ldap_err2string(rc) );
786 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
788 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
791 if (timelimit != -1 &&
792 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
794 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
797 if (sizelimit != -1 &&
798 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
800 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
805 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
806 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
808 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
809 referrals ? "on" : "off" );
813 if (version == -1 ) {
814 version = LDAP_VERSION3;
817 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
818 != LDAP_OPT_SUCCESS )
820 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
825 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
826 ldap_perror( ld, "ldap_start_tls" );
832 if ( pw_file || want_bindpw ) {
834 rc = lutil_get_filed_password( pw_file, &passwd );
835 if( rc ) return EXIT_FAILURE;
837 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
838 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
842 if ( authmethod == LDAP_AUTH_SASL ) {
843 #ifdef HAVE_CYRUS_SASL
846 if( sasl_secprops != NULL ) {
847 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
848 (void *) sasl_secprops );
850 if( rc != LDAP_OPT_SUCCESS ) {
852 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
854 return( EXIT_FAILURE );
858 defaults = lutil_sasl_defaults( ld,
865 rc = ldap_sasl_interactive_bind_s( ld, binddn,
866 sasl_mech, NULL, NULL,
867 sasl_flags, lutil_sasl_interact, defaults );
869 if( rc != LDAP_SUCCESS ) {
870 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
871 return( EXIT_FAILURE );
874 fprintf( stderr, "%s: not compiled with SASL support\n",
876 return( EXIT_FAILURE );
879 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
881 ldap_perror( ld, "ldap_bind" );
882 return( EXIT_FAILURE );
886 if ( manageDSAit || valuesReturnFilter ) {
890 LDAPControl *ctrls[3];
896 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
897 c1.ldctl_value.bv_val = NULL;
898 c1.ldctl_value.bv_len = 0;
899 c1.ldctl_iscritical = manageDSAit > 1;
902 if ( valuesReturnFilter ) {
906 c2.ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
907 c2.ldctl_iscritical = valuesReturnFilter > 1;
909 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
913 if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
915 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
919 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR ) {
923 c2.ldctl_value=(*bvalp);
926 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
931 if( err != LDAP_OPT_SUCCESS ) {
932 fprintf( stderr, "Could not set %scontrols\n",
933 (c1.ldctl_iscritical || c2.ldctl_iscritical)
934 ? "critical " : "" );
935 if( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
942 fprintf( stderr, "filter%s: %s\nrequesting: ",
943 infile != NULL ? " pattern" : "",
946 if ( attrs == NULL ) {
947 fprintf( stderr, "ALL" );
949 for ( i = 0; attrs[ i ] != NULL; ++i ) {
950 fprintf( stderr, "%s ", attrs[ i ] );
953 fprintf( stderr, "\n" );
957 printf( "# extended LDIF\n" );
958 } else if ( ldif < 3 ) {
959 printf( "version: %d\n\n", 1 );
963 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
965 infile != NULL ? " pattern" : "",
968 if ( attrs == NULL ) {
971 for ( i = 0; attrs[ i ] != NULL; ++i ) {
972 printf( "%s ", attrs[ i ] );
977 printf("\n# with manageDSAit %scontrol",
978 manageDSAit > 1 ? "critical " : "" );
984 if ( infile == NULL ) {
985 rc = dosearch( ld, base, scope, NULL, filtpattern,
986 attrs, attrsonly, NULL, NULL, NULL, -1 );
991 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
992 line[ strlen( line ) - 1 ] = '\0';
998 rc = dosearch( ld, base, scope, filtpattern, line,
999 attrs, attrsonly, NULL, NULL, NULL, -1 );
1001 if ( fp != stdin ) {
1011 static int dosearch(
1019 LDAPControl **sctrls,
1020 LDAPControl **cctrls,
1021 struct timeval *timeout,
1031 LDAPMessage *res, *msg;
1034 if( filtpatt != NULL ) {
1035 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1036 if( filter == NULL ) {
1038 return EXIT_FAILURE;
1041 sprintf( filter, filtpatt, value );
1044 fprintf( stderr, "filter: %s\n", filter );
1048 printf( "#\n# filter: %s\n#\n", filter );
1056 return LDAP_SUCCESS;
1059 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1060 sctrls, cctrls, timeout, sizelimit, &msgid );
1062 if ( filtpatt != NULL ) {
1066 if( rc != LDAP_SUCCESS ) {
1067 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1068 prog, ldap_err2string( rc ), rc );
1072 nresponses = nentries = nreferences = nextended = npartial = 0;
1076 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1077 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1081 (void) ldap_sort_entries( ld, &res,
1082 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1085 for ( msg = ldap_first_message( ld, res );
1087 msg = ldap_next_message( ld, msg ) )
1089 if( nresponses++ ) putchar('\n');
1091 switch( ldap_msgtype( msg ) ) {
1092 case LDAP_RES_SEARCH_ENTRY:
1094 print_entry( ld, msg, attrsonly );
1097 case LDAP_RES_SEARCH_REFERENCE:
1099 print_reference( ld, msg );
1102 case LDAP_RES_EXTENDED:
1104 print_extended( ld, msg );
1106 if( ldap_msgid( msg ) == 0 ) {
1107 /* unsolicited extended operation */
1112 case LDAP_RES_EXTENDED_PARTIAL:
1114 print_partial( ld, msg );
1117 case LDAP_RES_SEARCH_RESULT:
1118 rc = print_result( ld, msg, 1 );
1123 ldap_msgfree( res );
1127 ldap_perror( ld, "ldap_result" );
1133 printf( "\n# numResponses: %d\n", nresponses );
1134 if( nentries ) printf( "# numEntries: %d\n", nentries );
1135 if( nextended ) printf( "# numExtended: %d\n", nextended );
1136 if( npartial ) printf( "# numPartial: %d\n", npartial );
1137 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1150 char tmpfname[ 256 ];
1153 BerElement *ber = NULL;
1154 struct berval **bvals;
1155 LDAPControl **ctrls = NULL;
1158 dn = ldap_get_dn( ld, entry );
1162 ufn = ldap_dn2ufn( dn );
1163 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1165 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1167 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1169 if( rc != LDAP_SUCCESS ) {
1170 fprintf(stderr, "print_entry: %d\n", rc );
1171 ldap_perror( ld, "ldap_get_entry_controls" );
1172 exit( EXIT_FAILURE );
1176 print_ctrls( ctrls );
1177 ldap_controls_free( ctrls );
1182 ufn = ldap_dn2ufn( dn );
1184 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1187 if( ufn != NULL ) ldap_memfree( ufn );
1190 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1191 a = ldap_next_attribute( ld, entry, ber ) )
1194 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1196 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1197 for ( i = 0; bvals[i] != NULL; i++ ) {
1198 if ( vals2tmp > 1 || ( vals2tmp
1199 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1202 /* write value to file */
1203 snprintf( tmpfname, sizeof tmpfname,
1204 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1208 tmpfd = mkstemp( tmpfname );
1215 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1220 if ( fwrite( bvals[ i ]->bv_val,
1221 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1230 snprintf( url, sizeof url, "%s%s", urlpre,
1231 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1234 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1237 write_ldif( LDIF_PUT_VALUE, a,
1238 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1241 ber_bvecfree( bvals );
1250 static void print_reference(
1252 LDAPMessage *reference )
1256 LDAPControl **ctrls;
1259 printf("# search reference\n");
1262 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1264 if( rc != LDAP_SUCCESS ) {
1265 ldap_perror(ld, "ldap_parse_reference");
1266 exit( EXIT_FAILURE );
1271 for( i=0; refs[i] != NULL; i++ ) {
1272 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1273 "ref", refs[i], strlen(refs[i]) );
1275 ber_memvfree( (void **) refs );
1279 print_ctrls( ctrls );
1280 ldap_controls_free( ctrls );
1284 static void print_extended(
1286 LDAPMessage *extended )
1289 char *retoid = NULL;
1290 struct berval *retdata = NULL;
1293 printf("# extended result response\n");
1296 rc = ldap_parse_extended_result( ld, extended,
1297 &retoid, &retdata, 0 );
1299 if( rc != LDAP_SUCCESS ) {
1300 ldap_perror(ld, "ldap_parse_extended_result");
1301 exit( EXIT_FAILURE );
1304 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1305 "extended", retoid, retoid ? strlen(retoid) : 0 );
1306 ber_memfree( retoid );
1309 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1310 "data", retdata->bv_val, retdata->bv_len );
1311 ber_bvfree( retdata );
1314 print_result( ld, extended, 0 );
1317 static void print_partial(
1319 LDAPMessage *partial )
1322 char *retoid = NULL;
1323 struct berval *retdata = NULL;
1324 LDAPControl **ctrls = NULL;
1327 printf("# extended partial response\n");
1330 rc = ldap_parse_extended_partial( ld, partial,
1331 &retoid, &retdata, &ctrls, 0 );
1333 if( rc != LDAP_SUCCESS ) {
1334 ldap_perror(ld, "ldap_parse_extended_partial");
1335 exit( EXIT_FAILURE );
1338 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1339 "partial", retoid, retoid ? strlen(retoid) : 0 );
1341 ber_memfree( retoid );
1344 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1346 retdata->bv_val, retdata->bv_len );
1348 ber_bvfree( retdata );
1352 print_ctrls( ctrls );
1353 ldap_controls_free( ctrls );
1357 static int print_result(
1359 LDAPMessage *result, int search )
1363 char *matcheddn = NULL;
1366 LDAPControl **ctrls = NULL;
1370 printf("# search result\n");
1373 printf("%s: %d\n", "search", ldap_msgid(result) );
1377 rc = ldap_parse_result( ld, result,
1378 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1380 if( rc != LDAP_SUCCESS ) {
1381 ldap_perror(ld, "ldap_parse_result");
1382 exit( EXIT_FAILURE );
1387 printf( "result: %d %s\n", err, ldap_err2string(err) );
1389 } else if ( err != LDAP_SUCCESS ) {
1390 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1393 if( matcheddn && *matcheddn ) {
1395 write_ldif( LDIF_PUT_VALUE,
1396 "matchedDN", matcheddn, strlen(matcheddn) );
1398 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1401 ber_memfree( matcheddn );
1404 if( text && *text ) {
1406 write_ldif( LDIF_PUT_TEXT, "text",
1407 text, strlen(text) );
1409 fprintf( stderr, "Additional information: %s\n", text );
1412 ber_memfree( text );
1417 for( i=0; refs[i] != NULL; i++ ) {
1419 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1421 fprintf( stderr, "Referral: %s\n", refs[i] );
1425 ber_memvfree( (void **) refs );
1429 print_ctrls( ctrls );
1430 ldap_controls_free( ctrls );
1436 static void print_ctrls(
1437 LDAPControl **ctrls )
1440 for(i=0; ctrls[i] != NULL; i++ ) {
1441 /* control: OID criticality base64value */
1442 struct berval *b64 = NULL;
1446 len = strlen( ctrls[i]->ldctl_oid );
1448 /* add enough for space after OID and the critical value itself */
1449 len += ctrls[i]->ldctl_iscritical
1450 ? sizeof("true") : sizeof("false");
1452 /* convert to base64 */
1453 if( ctrls[i]->ldctl_value.bv_len ) {
1454 b64 = ber_memalloc( sizeof(struct berval) );
1456 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1457 ctrls[i]->ldctl_value.bv_len ) + 1;
1458 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1460 b64->bv_len = lutil_b64_ntop(
1461 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1462 b64->bv_val, b64->bv_len );
1466 len += 1 + b64->bv_len;
1469 str = malloc( len + 1 );
1470 strcpy( str, ctrls[i]->ldctl_oid );
1471 strcat( str, ctrls[i]->ldctl_iscritical
1472 ? " true" : " false" );
1476 strcat(str, b64->bv_val );
1479 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1480 "control", str, len );
1488 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1492 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1496 fputs( ldif, stdout );
1497 ber_memfree( ldif );