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>
33 #include "ldap_defaults.h"
39 usage( const char *s )
42 "usage: %s [options] filter [attributes...]\nwhere:\n"
43 " filter\tRFC-1558 compliant LDAP search filter\n"
44 " attributes\twhitespace-separated list of attributes to retrieve\n"
45 "\t\t1.1 -- no attributes\n"
46 "\t\t* -- all user attributes\n"
47 "\t\t+ -- all operational attributes\n"
48 "\t\tempty list -- all non-operational attributes\n"
50 " -a deref\tone of `never', `always', `search', or `find' (alias\n"
52 " -A\t\tretrieve attribute names only (no values)\n"
53 " -b basedn\tbase dn for search\n"
54 " -d level\tset LDAP debugging level to `level'\n"
55 " -D binddn\tbind DN\n"
56 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
57 " -f file\t\tperform sequence of searches listed in `file'\n"
58 " -h host\t\tLDAP server\n"
59 " -I\t\trequest SASL integrity checking (-II to make it\n"
61 " -k\t\tuse Kerberos authentication\n"
62 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
63 " -l limit\ttime limit (in seconds) for search\n"
64 " -L\t\tprint entries in LDIF format (default)\n"
65 " -LL\t\tprint entries in LDIF format without comments\n"
66 " -LLL\t\tprint entries in LDIF format without comments and\n"
68 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
69 " -n\t\tshow what would be done but don't actually search\n"
70 " -p port\t\tport on LDAP server\n"
71 " -P version\tprocotol version (2 or 3)\n"
72 " -R\t\tdo not automatically follow referrals\n"
73 " -s scope\tone of base, one, or sub (search scope)\n"
74 " -S attr\t\tsort the results by attribute `attr'\n"
75 " -t\t\twrite binary values to files in TMPDIR\n"
76 " -tt\t\twrite all values to files in TMPDIR\n"
77 " -T path\t\twrite files to directory specified by path (default:\n"
79 " -u\t\tinclude User Friendly entry names in the output\n"
80 " -U user\t\tSASL authentication identity (username)\n"
81 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
82 " -V prefix\tURL prefix for files (default: \"file://tmp/\")\n"
83 " -w passwd\tbind passwd (for simple authentication)\n"
84 " -W\t\tprompt for bind passwd\n"
85 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
86 " -Y mech\t\tSASL mechanism\n"
87 " -z limit\tsize limit (in entries) for search\n"
88 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
94 static void print_entry LDAP_P((
99 static int write_ldif LDAP_P((
105 static int dosearch LDAP_P((
114 #define TMPDIR "/tmp"
115 #define URLPRE "file:/tmp/"
117 static char *tmpdir = NULL;
118 static char *urlpre = NULL;
120 static char *binddn = NULL;
121 static struct berval passwd = { 0, NULL };
122 static char *base = NULL;
123 static char *ldaphost = NULL;
124 static int ldapport = 0;
125 #ifdef HAVE_CYRUS_SASL
126 static char *sasl_authc_id = NULL;
127 static char *sasl_authz_id = NULL;
128 static char *sasl_mech = NULL;
129 static int sasl_integrity = 0;
130 static int sasl_privacy = 0;
132 static int use_tls = 0;
133 static char *sep = DEFSEP;
134 static char *sortattr = NULL;
135 static int skipsortattr = 0;
136 static int verbose, not, includeufn, binary, vals2tmp, ldif;
139 main( int argc, char **argv )
141 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
143 int rc, i, first, scope, deref, attrsonly, manageDSAit;
144 int referrals, timelimit, sizelimit, debug;
145 int authmethod, version, want_bindpw;
149 debug = verbose = binary = not = vals2tmp =
150 attrsonly = manageDSAit = ldif = want_bindpw = 0;
152 deref = sizelimit = timelimit = version = -1;
154 /* default should be off */
157 scope = LDAP_SCOPE_SUBTREE;
158 authmethod = LDAP_AUTH_SIMPLE;
160 while (( i = getopt( argc, argv,
161 "Aa:Bb:D:d:EF:f:h:IKkLl:MnP:p:RS:s:T:tU:uV:vWw:X:Y:Zz:")) != EOF )
164 case 'n': /* do Not do any searches */
167 case 'v': /* verbose mode */
171 debug |= atoi( optarg );
173 case 'k': /* use kerberos bind */
174 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
175 authmethod = LDAP_AUTH_KRBV4;
177 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
178 return( EXIT_FAILURE );
181 case 'K': /* use kerberos bind, 1st part only */
182 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
183 authmethod = LDAP_AUTH_KRBV41;
185 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
186 return( EXIT_FAILURE );
190 case 'u': /* include UFN */
193 case 't': /* write attribute values to /tmp files */
197 /* enable Manage DSA IT */
200 case 'R': /* don't automatically chase referrals */
203 case 'A': /* retrieve attribute names only -- no values */
206 case 'L': /* print entries in LDIF format */
208 /* fall through -- always allow binary when outputting LDIF */
209 case 'B': /* allow binary values to be printed */
212 case 's': /* search scope */
213 if ( strcasecmp( optarg, "base" ) == 0 ) {
214 scope = LDAP_SCOPE_BASE;
215 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
216 scope = LDAP_SCOPE_ONELEVEL;
217 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
218 scope = LDAP_SCOPE_SUBTREE;
220 fprintf( stderr, "scope should be base, one, or sub\n" );
225 case 'a': /* set alias deref option */
226 if ( strcasecmp( optarg, "never" ) == 0 ) {
227 deref = LDAP_DEREF_NEVER;
228 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
229 deref = LDAP_DEREF_SEARCHING;
230 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
231 deref = LDAP_DEREF_FINDING;
232 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
233 deref = LDAP_DEREF_ALWAYS;
235 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
240 case 'T': /* field separator */
241 if( tmpdir ) free( tmpdir );
242 tmpdir = strdup( optarg );
244 case 'V': /* field separator */
245 if( urlpre ) free( urlpre );
246 urlpre = strdup( optarg );
248 case 'F': /* field separator */
249 sep = strdup( optarg );
251 case 'f': /* input file */
252 infile = strdup( optarg );
254 case 'h': /* ldap host */
255 ldaphost = strdup( optarg );
257 case 'b': /* searchbase */
258 base = strdup( optarg );
260 case 'D': /* bind DN */
261 binddn = strdup( optarg );
263 case 'p': /* ldap port */
264 ldapport = atoi( optarg );
266 case 'w': /* bind password */
267 passwd.bv_val = strdup( optarg );
271 for( p = optarg; *p == '\0'; p++ ) {
275 passwd.bv_len = strlen( passwd.bv_val );
277 case 'l': /* time limit */
278 timelimit = atoi( optarg );
280 case 'z': /* size limit */
281 sizelimit = atoi( optarg );
283 case 'S': /* sort attribute */
284 sortattr = strdup( optarg );
290 switch( atoi( optarg ) )
293 version = LDAP_VERSION2;
296 version = LDAP_VERSION3;
299 fprintf( stderr, "protocol version should be 2 or 3\n" );
304 #ifdef HAVE_CYRUS_SASL
306 authmethod = LDAP_AUTH_SASL;
308 fprintf( stderr, "%s was not compiled with SASL support\n",
310 return( EXIT_FAILURE );
314 #ifdef HAVE_CYRUS_SASL
316 authmethod = LDAP_AUTH_SASL;
318 fprintf( stderr, "%s was not compiled with SASL support\n",
320 return( EXIT_FAILURE );
324 #ifdef HAVE_CYRUS_SASL
325 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
326 sasl_mech = strdup( optarg );
328 authmethod = LDAP_AUTH_SASL;
330 fprintf( stderr, "%s was not compiled with SASL support\n",
332 return( EXIT_FAILURE );
336 #ifdef HAVE_CYRUS_SASL
337 sasl_authc_id = strdup( optarg );
338 authmethod = LDAP_AUTH_SASL;
340 fprintf( stderr, "%s was not compiled with SASL support\n",
342 return( EXIT_FAILURE );
346 #ifdef HAVE_CYRUS_SASL
347 sasl_authz_id = strdup( optarg );
348 authmethod = LDAP_AUTH_SASL;
350 fprintf( stderr, "%s was not compiled with SASL support\n",
352 return( EXIT_FAILURE );
359 fprintf( stderr, "%s was not compiled with TLS support\n",
361 return( EXIT_FAILURE );
371 /* no alternative format */
372 if( ldif == 0 ) ldif = 1;
375 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
376 LDAP_AUTH_KRBV41 ) ) {
377 if( version > LDAP_VERSION2 ) {
378 fprintf( stderr, "Kerberos requires LDAPv2\n" );
379 return( EXIT_FAILURE );
381 version = LDAP_VERSION2;
383 else if ( authmethod == LDAP_AUTH_SASL ) {
384 if( version != -1 && version != LDAP_VERSION3 ) {
385 fprintf( stderr, "SASL requires LDAPv3\n" );
386 return( EXIT_FAILURE );
388 version = LDAP_VERSION3;
392 if( version != -1 && version != LDAP_VERSION3 ) {
393 fprintf(stderr, "manage DSA control requires LDAPv3\n");
396 version = LDAP_VERSION3;
400 if( version != -1 && version != LDAP_VERSION3 ) {
401 fprintf(stderr, "Start TLS requires LDAPv3\n");
404 version = LDAP_VERSION3;
407 if ( argc - optind < 1 ) {
411 filtpattern = strdup( argv[ optind ] );
413 if ( argv[ optind + 1 ] == NULL ) {
415 } else if ( sortattr == NULL || *sortattr == '\0' ) {
416 attrs = &argv[ optind + 1 ];
418 for ( i = optind + 1; i < argc; i++ ) {
419 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
425 argv[ optind ] = sortattr;
429 attrs = &argv[ optind ];
432 if ( infile != NULL ) {
433 if ( infile[0] == '-' && infile[1] == '\0' ) {
435 } else if (( fp = fopen( infile, "r" )) == NULL ) {
442 && (tmpdir = getenv("TMPDIR")) == NULL
443 && (tmpdir = getenv("TMP")) == NULL
444 && (tmpdir = getenv("TEMP")) == NULL )
449 if( urlpre == NULL ) {
450 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
452 if( urlpre == NULL ) {
457 sprintf( urlpre, "file:///%s/",
458 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
460 /* urlpre should be URLized.... */
464 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
465 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
467 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
468 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
474 (void) SIGNAL( SIGPIPE, SIG_IGN );
479 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
480 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
484 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
485 perror( "ldap_init" );
486 return( EXIT_FAILURE );
490 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
492 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
494 if (timelimit != -1 &&
495 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
497 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
499 if (sizelimit != -1 &&
500 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
502 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
504 if (referrals != -1 &&
505 ldap_set_option( ld, LDAP_OPT_REFERRALS,
506 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
508 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
509 referrals ? "on" : "off" );
513 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
515 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
518 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
520 ldap_perror( ld, "ldap_start_tls" );
521 return( EXIT_FAILURE );
526 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
527 passwd.bv_len = strlen( passwd.bv_val );
530 if ( authmethod == LDAP_AUTH_SASL ) {
531 #ifdef HAVE_CYRUS_SASL
532 int minssf = 0, maxssf = 0;
534 if ( sasl_integrity > 0 )
536 if ( sasl_integrity > 1 )
538 if ( sasl_privacy > 0 )
539 maxssf = 100000; /* Something big value */
540 if ( sasl_privacy > 1 )
543 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
544 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
545 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
547 return( EXIT_FAILURE );
549 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
550 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
551 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
553 return( EXIT_FAILURE );
556 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
557 sasl_authz_id, sasl_mech,
558 passwd.bv_len ? &passwd : NULL,
561 if( rc != LDAP_SUCCESS ) {
562 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
563 return( EXIT_FAILURE );
566 fprintf( stderr, "%s was not compiled with SASL support\n",
568 return( EXIT_FAILURE );
572 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
574 ldap_perror( ld, "ldap_bind" );
575 return( EXIT_FAILURE );
582 LDAPControl *ctrls[2];
586 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
587 c.ldctl_value.bv_val = NULL;
588 c.ldctl_value.bv_len = 0;
589 c.ldctl_iscritical = manageDSAit > 1;
591 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
593 if( err != LDAP_OPT_SUCCESS ) {
594 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
595 if( c.ldctl_iscritical ) {
596 exit( EXIT_FAILURE );
602 fprintf( stderr, "filter%s: %s\nreturning: ",
603 infile != NULL ? " pattern" : "",
606 if ( attrs == NULL ) {
607 fprintf( stderr, "ALL" );
609 for ( i = 0; attrs[ i ] != NULL; ++i ) {
610 fprintf( stderr, "%s ", attrs[ i ] );
613 fprintf( stderr, "\n" );
618 printf( "version: 1\n\n");
622 printf( "#\n# filter%s: %s\n# returning: ",
623 infile != NULL ? " pattern" : "",
626 if ( attrs == NULL ) {
629 for ( i = 0; attrs[ i ] != NULL; ++i ) {
630 printf( "%s ", attrs[ i ] );
637 if ( infile == NULL ) {
638 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
643 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
644 line[ strlen( line ) - 1 ] = '\0';
650 rc = dosearch( ld, base, scope, attrs, attrsonly,
672 char filter[ BUFSIZ ];
673 int rc, first, matches;
674 LDAPMessage *res, *e;
676 if( filtpatt != NULL ) {
677 sprintf( filter, filtpatt, value );
680 fprintf( stderr, "filter is: (%s)\n", filter );
684 printf( "#\n# filter: %s\n#\n", filter );
688 sprintf( filter, "%s", value );
692 return( LDAP_SUCCESS );
695 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
697 ldap_perror( ld, "ldap_search" );
699 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
706 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
707 == LDAP_RES_SEARCH_ENTRY ) {
709 e = ldap_first_entry( ld, res );
715 print_entry( ld, e, attrsonly );
720 ldap_perror( ld, "ldap_result" );
723 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
724 ldap_perror( ld, "ldap_search" );
726 if ( sortattr != NULL ) {
727 (void) ldap_sort_entries( ld, &res,
728 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
731 for ( e = ldap_first_entry( ld, res ); e != NULL;
732 e = ldap_next_entry( ld, e ) ) {
739 print_entry( ld, e, attrsonly );
744 printf( "%d matches\n", matches );
759 char tmpfname[ 256 ];
762 BerElement *ber = NULL;
763 struct berval **bvals;
766 dn = ldap_get_dn( ld, entry );
770 ufn = ldap_dn2ufn( dn );
771 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
774 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
776 printf( "%s\n", dn );
781 ufn = ldap_dn2ufn( dn );
784 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
786 printf( "%s\n", ufn );
790 if( ufn != NULL ) ldap_memfree( ufn );
793 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
794 a = ldap_next_attribute( ld, entry, ber ) )
796 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
802 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
807 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
808 for ( i = 0; bvals[i] != NULL; i++ ) {
809 if ( vals2tmp > 1 || ( vals2tmp
810 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
813 /* write value to file */
814 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
818 if ( mktemp( tmpfname ) == NULL ) {
823 if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
828 if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
833 if ( fwrite( bvals[ i ]->bv_val,
834 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
843 sprintf( url, "%s%s", urlpre,
844 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
847 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
849 printf( "%s%s%s\n", a, sep, url );
855 write_ldif( LDIF_PUT_VALUE, a,
856 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
859 int notprint = !binary && !vals2tmp
860 && ldif_is_not_printable( bvals[i]->bv_val,
862 printf( "%s%s", a, sep );
863 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
867 ber_bvecfree( bvals );
878 write_ldif( int type, char *name, char *value, ber_len_t vallen )
882 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
886 fputs( ldif, stdout );