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 mech SASL mechanism\n"
94 " -Z Start TLS request (-ZZ to require successful response)\n"
95 , s, def_urlpre, def_tmpdir );
100 static void print_entry LDAP_P((
105 static void print_reference(
107 LDAPMessage *reference );
109 static void print_extended(
111 LDAPMessage *extended );
113 static void print_partial(
115 LDAPMessage *partial );
117 static int print_result(
122 static void print_ctrls(
123 LDAPControl **ctrls );
125 static int write_ldif LDAP_P((
131 static int dosearch LDAP_P((
139 LDAPControl **sctrls,
140 LDAPControl **cctrls,
141 struct timeval *timeout,
144 static char *tmpdir = NULL;
145 static char *urlpre = NULL;
146 static char *prog = NULL;
147 static char *binddn = NULL;
148 static struct berval passwd = { 0, NULL };
149 static char *base = NULL;
150 static char *ldaphost = NULL;
151 static char *ldapuri = NULL;
152 static int ldapport = 0;
153 #ifdef HAVE_CYRUS_SASL
154 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
155 static char *sasl_realm = NULL;
156 static char *sasl_authc_id = NULL;
157 static char *sasl_authz_id = NULL;
158 static char *sasl_mech = NULL;
159 static char *sasl_secprops = NULL;
161 static int use_tls = 0;
162 static char *sortattr = NULL;
163 static int verbose, not, includeufn, vals2tmp, ldif;
170 if (*LDAP_DIRSEP != '/')
172 for (p = url; *p; p++)
174 if (*p == *LDAP_DIRSEP)
181 main( int argc, char **argv )
183 char *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
185 int rc, i, first, scope, deref, attrsonly, manageDSAit;
186 int referrals, timelimit, sizelimit, debug;
187 int authmethod, version, want_bindpw;
189 int valuesReturnFilter;
191 struct berval *bvalp;
192 char *vrFilter = NULL, *control = NULL, *s;
196 debug = verbose = not = vals2tmp = referrals = valuesReturnFilter =
197 attrsonly = manageDSAit = ldif = want_bindpw = 0;
199 lutil_log_initialize(argc, argv);
201 deref = sizelimit = timelimit = version = -1;
203 scope = LDAP_SCOPE_SUBTREE;
206 if((def_tmpdir = getenv("TMPDIR")) == NULL &&
207 (def_tmpdir = getenv("TMP")) == NULL &&
208 (def_tmpdir = getenv("TEMP")) == NULL )
210 def_tmpdir = LDAP_TMPDIR;
214 def_tmpdir = LDAP_TMPDIR;
216 def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
218 if( def_urlpre == NULL ) {
223 sprintf( def_urlpre, "file:///%s/",
224 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
226 urlize( def_urlpre );
228 prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
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: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 );
609 #ifdef HAVE_CYRUS_SASL
610 if( sasl_mech != NULL ) {
611 fprintf( stderr, "%s: -Y previously specified\n", prog );
614 if( version == LDAP_VERSION2 ) {
615 fprintf( stderr, "%s: -Y incompatible with version %d\n",
619 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
620 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
623 authmethod = LDAP_AUTH_SASL;
624 version = LDAP_VERSION3;
625 sasl_mech = strdup( optarg );
627 fprintf( stderr, "%s: not compiled with SASL support\n",
629 return( EXIT_FAILURE );
633 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
634 fprintf( stderr, "%s: incompatible with previous "
635 "authentication choice\n", prog );
638 authmethod = LDAP_AUTH_SIMPLE;
641 #ifdef HAVE_CYRUS_SASL
642 if( sasl_authz_id != NULL ) {
643 fprintf( stderr, "%s: -X previously specified\n", prog );
646 if( version == LDAP_VERSION2 ) {
647 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
651 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
652 fprintf( stderr, "%s: -X incompatible with "
653 "authentication choice\n", prog );
656 authmethod = LDAP_AUTH_SASL;
657 version = LDAP_VERSION3;
658 sasl_authz_id = strdup( optarg );
660 fprintf( stderr, "%s: not compiled with SASL support\n",
662 return( EXIT_FAILURE );
667 if( version == LDAP_VERSION2 ) {
668 fprintf( stderr, "%s: -Z incompatible with version %d\n",
672 version = LDAP_VERSION3;
675 fprintf( stderr, "%s: not compiled with TLS support\n",
677 return( EXIT_FAILURE );
681 fprintf( stderr, "%s: unrecognized option -%c\n",
688 version = LDAP_VERSION3;
690 if (authmethod == -1 && version > LDAP_VERSION2) {
691 #ifdef HAVE_CYRUS_SASL
692 authmethod = LDAP_AUTH_SASL;
694 authmethod = LDAP_AUTH_SIMPLE;
698 if (( argc - optind < 1 ) ||
699 ( *argv[optind] != '(' /*')'*/ &&
700 ( strchr( argv[optind], '=' ) == NULL ) ) )
702 filtpattern = "(objectclass=*)";
704 filtpattern = strdup( argv[optind++] );
707 if ( argv[optind] != NULL ) {
708 attrs = &argv[optind];
711 if ( infile != NULL ) {
712 if ( infile[0] == '-' && infile[1] == '\0' ) {
714 } else if (( fp = fopen( infile, "r" )) == NULL ) {
720 if ( tmpdir == NULL ) {
723 if ( urlpre == NULL )
727 if( urlpre == NULL ) {
728 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
730 if( urlpre == NULL ) {
735 sprintf( urlpre, "file:///%s/",
736 tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
742 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
743 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
745 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
746 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
752 (void) SIGNAL( SIGPIPE, SIG_IGN );
755 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
757 fprintf( stderr, "ldap_init( %s, %d )\n",
758 ldaphost != NULL ? ldaphost : "<DEFAULT>",
762 ld = ldap_init( ldaphost, ldapport );
764 perror("ldapsearch: ldap_init");
770 fprintf( stderr, "ldap_initialize( %s )\n",
771 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
774 rc = ldap_initialize( &ld, ldapuri );
775 if( rc != LDAP_SUCCESS ) {
776 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
777 rc, ldap_err2string(rc) );
783 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
785 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
788 if (timelimit != -1 &&
789 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
791 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
794 if (sizelimit != -1 &&
795 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
797 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
802 if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
803 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
805 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
806 referrals ? "on" : "off" );
810 if (version == -1 ) {
811 version = LDAP_VERSION3;
814 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
815 != LDAP_OPT_SUCCESS )
817 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
822 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
823 ldap_perror( ld, "ldap_start_tls" );
830 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
831 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
834 if ( authmethod == LDAP_AUTH_SASL ) {
835 #ifdef HAVE_CYRUS_SASL
838 if( sasl_secprops != NULL ) {
839 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
840 (void *) sasl_secprops );
842 if( rc != LDAP_OPT_SUCCESS ) {
844 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
846 return( EXIT_FAILURE );
850 defaults = lutil_sasl_defaults( ld,
857 rc = ldap_sasl_interactive_bind_s( ld, binddn,
858 sasl_mech, NULL, NULL,
859 sasl_flags, lutil_sasl_interact, defaults );
861 if( rc != LDAP_SUCCESS ) {
862 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
863 return( EXIT_FAILURE );
866 fprintf( stderr, "%s: not compiled with SASL support\n",
868 return( EXIT_FAILURE );
871 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
873 ldap_perror( ld, "ldap_bind" );
874 return( EXIT_FAILURE );
878 if ( manageDSAit || valuesReturnFilter ) {
882 LDAPControl *ctrls[3];
888 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
889 c1.ldctl_value.bv_val = NULL;
890 c1.ldctl_value.bv_len = 0;
891 c1.ldctl_iscritical = manageDSAit > 1;
894 if ( valuesReturnFilter ) {
895 struct berval *bvalp;
899 c2.ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
900 c2.ldctl_iscritical = valuesReturnFilter > 1;
902 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL )
903 exit( EXIT_FAILURE );
905 if ( err = put_vrFilter(ber, vrFilter)==-1 ) {
907 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
908 exit( EXIT_FAILURE );
911 if ( ber_flatten( ber, &bvalp ) == LBER_ERROR )
912 return LDAP_NO_MEMORY;
914 c2.ldctl_value=(*bvalp);
918 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
923 if( err != LDAP_OPT_SUCCESS ) {
924 fprintf( stderr, "Could not set %scontrols\n",
925 (c1.ldctl_iscritical || c2.ldctl_iscritical)
926 ? "critical " : "" );
927 if( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
928 exit( EXIT_FAILURE );
934 fprintf( stderr, "filter%s: %s\nrequesting: ",
935 infile != NULL ? " pattern" : "",
938 if ( attrs == NULL ) {
939 fprintf( stderr, "ALL" );
941 for ( i = 0; attrs[ i ] != NULL; ++i ) {
942 fprintf( stderr, "%s ", attrs[ i ] );
945 fprintf( stderr, "\n" );
949 printf( "# extended LDIF\n" );
950 } else if ( ldif < 3 ) {
951 printf( "version: %d\n\n", 1 );
955 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
957 infile != NULL ? " pattern" : "",
960 if ( attrs == NULL ) {
963 for ( i = 0; attrs[ i ] != NULL; ++i ) {
964 printf( "%s ", attrs[ i ] );
969 printf("\n# with manageDSAit %scontrol",
970 manageDSAit > 1 ? "critical " : "" );
976 if ( infile == NULL ) {
977 rc = dosearch( ld, base, scope, NULL, filtpattern,
978 attrs, attrsonly, NULL, NULL, NULL, -1 );
983 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
984 line[ strlen( line ) - 1 ] = '\0';
990 rc = dosearch( ld, base, scope, filtpattern, line,
991 attrs, attrsonly, NULL, NULL, NULL, -1 );
1003 static int dosearch(
1011 LDAPControl **sctrls,
1012 LDAPControl **cctrls,
1013 struct timeval *timeout,
1023 LDAPMessage *res, *msg;
1026 if( filtpatt != NULL ) {
1027 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1028 if( filter == NULL ) {
1030 return EXIT_FAILURE;
1033 sprintf( filter, filtpatt, value );
1036 fprintf( stderr, "filter: %s\n", filter );
1040 printf( "#\n# filter: %s\n#\n", filter );
1048 return LDAP_SUCCESS;
1051 rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1052 sctrls, cctrls, timeout, sizelimit, &msgid );
1054 if ( filtpatt != NULL ) {
1058 if( rc != LDAP_SUCCESS ) {
1059 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1060 prog, ldap_err2string( rc ), rc );
1064 nresponses = nentries = nreferences = nextended = npartial = 0;
1068 while ((rc = ldap_result( ld, LDAP_RES_ANY,
1069 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1073 (void) ldap_sort_entries( ld, &res,
1074 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1077 for ( msg = ldap_first_message( ld, res );
1079 msg = ldap_next_message( ld, msg ) )
1081 if( nresponses++ ) putchar('\n');
1083 switch( ldap_msgtype( msg ) ) {
1084 case LDAP_RES_SEARCH_ENTRY:
1086 print_entry( ld, msg, attrsonly );
1089 case LDAP_RES_SEARCH_REFERENCE:
1091 print_reference( ld, msg );
1094 case LDAP_RES_EXTENDED:
1096 print_extended( ld, msg );
1098 if( ldap_msgid( msg ) == 0 ) {
1099 /* unsolicited extended operation */
1104 case LDAP_RES_EXTENDED_PARTIAL:
1106 print_partial( ld, msg );
1109 case LDAP_RES_SEARCH_RESULT:
1110 rc = print_result( ld, msg, 1 );
1115 ldap_msgfree( res );
1119 ldap_perror( ld, "ldap_result" );
1125 printf( "\n# numResponses: %d\n", nresponses );
1126 if( nentries ) printf( "# numEntries: %d\n", nentries );
1127 if( nextended ) printf( "# numExtended: %d\n", nextended );
1128 if( npartial ) printf( "# numPartial: %d\n", npartial );
1129 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1142 char tmpfname[ 256 ];
1145 BerElement *ber = NULL;
1146 struct berval **bvals;
1147 LDAPControl **ctrls = NULL;
1150 dn = ldap_get_dn( ld, entry );
1154 ufn = ldap_dn2ufn( dn );
1155 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1157 write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1159 rc = ldap_get_entry_controls( ld, entry, &ctrls );
1161 if( rc != LDAP_SUCCESS ) {
1162 fprintf(stderr, "print_entry: %d\n", rc );
1163 ldap_perror( ld, "ldap_get_entry_controls" );
1164 exit( EXIT_FAILURE );
1168 print_ctrls( ctrls );
1169 ldap_controls_free( ctrls );
1174 ufn = ldap_dn2ufn( dn );
1176 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1179 if( ufn != NULL ) ldap_memfree( ufn );
1182 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1183 a = ldap_next_attribute( ld, entry, ber ) )
1186 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1188 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1189 for ( i = 0; bvals[i] != NULL; i++ ) {
1190 if ( vals2tmp > 1 || ( vals2tmp
1191 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1194 /* write value to file */
1195 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1199 tmpfd = mkstemp( tmpfname );
1206 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1211 if ( fwrite( bvals[ i ]->bv_val,
1212 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1221 sprintf( url, "%s%s", urlpre,
1222 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1225 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1228 write_ldif( LDIF_PUT_VALUE, a,
1229 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1232 ber_bvecfree( bvals );
1241 static void print_reference(
1243 LDAPMessage *reference )
1247 LDAPControl **ctrls;
1250 printf("# search reference\n");
1253 rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1255 if( rc != LDAP_SUCCESS ) {
1256 ldap_perror(ld, "ldap_parse_reference");
1257 exit( EXIT_FAILURE );
1262 for( i=0; refs[i] != NULL; i++ ) {
1263 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1264 "ref", refs[i], strlen(refs[i]) );
1266 ber_memvfree( (void **) refs );
1270 print_ctrls( ctrls );
1271 ldap_controls_free( ctrls );
1275 static void print_extended(
1277 LDAPMessage *extended )
1280 char *retoid = NULL;
1281 struct berval *retdata = NULL;
1284 printf("# extended result response\n");
1287 rc = ldap_parse_extended_result( ld, extended,
1288 &retoid, &retdata, 0 );
1290 if( rc != LDAP_SUCCESS ) {
1291 ldap_perror(ld, "ldap_parse_extended_result");
1292 exit( EXIT_FAILURE );
1295 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1296 "extended", retoid, retoid ? strlen(retoid) : 0 );
1297 ber_memfree( retoid );
1300 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1301 "data", retdata->bv_val, retdata->bv_len );
1302 ber_bvfree( retdata );
1305 print_result( ld, extended, 0 );
1308 static void print_partial(
1310 LDAPMessage *partial )
1313 char *retoid = NULL;
1314 struct berval *retdata = NULL;
1315 LDAPControl **ctrls = NULL;
1318 printf("# extended partial response\n");
1321 rc = ldap_parse_extended_partial( ld, partial,
1322 &retoid, &retdata, &ctrls, 0 );
1324 if( rc != LDAP_SUCCESS ) {
1325 ldap_perror(ld, "ldap_parse_extended_partial");
1326 exit( EXIT_FAILURE );
1329 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1330 "partial", retoid, retoid ? strlen(retoid) : 0 );
1332 ber_memfree( retoid );
1335 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1337 retdata->bv_val, retdata->bv_len );
1339 ber_bvfree( retdata );
1343 print_ctrls( ctrls );
1344 ldap_controls_free( ctrls );
1348 static int print_result(
1350 LDAPMessage *result, int search )
1354 char *matcheddn = NULL;
1357 LDAPControl **ctrls = NULL;
1361 printf("# search result\n");
1364 printf("%s: %d\n", "search", ldap_msgid(result) );
1368 rc = ldap_parse_result( ld, result,
1369 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1371 if( rc != LDAP_SUCCESS ) {
1372 ldap_perror(ld, "ldap_parse_result");
1373 exit( EXIT_FAILURE );
1378 printf( "result: %d %s\n", err, ldap_err2string(err) );
1380 } else if ( err != LDAP_SUCCESS ) {
1381 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1384 if( matcheddn && *matcheddn ) {
1386 write_ldif( LDIF_PUT_VALUE,
1387 "matchedDN", matcheddn, strlen(matcheddn) );
1389 fprintf( stderr, "Matched DN: %s\n", matcheddn );
1392 ber_memfree( matcheddn );
1395 if( text && *text ) {
1397 write_ldif( LDIF_PUT_TEXT, "text",
1398 text, strlen(text) );
1400 fprintf( stderr, "Additional information: %s\n", text );
1403 ber_memfree( text );
1408 for( i=0; refs[i] != NULL; i++ ) {
1410 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1412 fprintf( stderr, "Referral: %s\n", refs[i] );
1416 ber_memvfree( (void **) refs );
1420 print_ctrls( ctrls );
1421 ldap_controls_free( ctrls );
1427 static void print_ctrls(
1428 LDAPControl **ctrls )
1431 for(i=0; ctrls[i] != NULL; i++ ) {
1432 /* control: OID criticality base64value */
1433 struct berval *b64 = NULL;
1437 len = strlen( ctrls[i]->ldctl_oid );
1439 /* add enough for space after OID and the critical value itself */
1440 len += ctrls[i]->ldctl_iscritical
1441 ? sizeof("true") : sizeof("false");
1443 /* convert to base64 */
1444 if( ctrls[i]->ldctl_value.bv_len ) {
1445 b64 = ber_memalloc( sizeof(struct berval) );
1447 b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1448 ctrls[i]->ldctl_value.bv_len ) + 1;
1449 b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1451 b64->bv_len = lutil_b64_ntop(
1452 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1453 b64->bv_val, b64->bv_len );
1457 len += 1 + b64->bv_len;
1460 str = malloc( len + 1 );
1461 strcpy( str, ctrls[i]->ldctl_oid );
1462 strcat( str, ctrls[i]->ldctl_iscritical
1463 ? " true" : " false" );
1467 strcat(str, b64->bv_val );
1470 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1471 "control", str, len );
1479 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1483 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1487 fputs( ldif, stdout );
1488 ber_memfree( ldif );