3 * Copyright 1998-1999 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>
25 #include "ldap_defaults.h"
30 usage( const char *s )
33 "usage: %s [options] filter [attributes...]\nwhere:\n"
34 " filter\tRFC-1558 compliant LDAP search filter\n"
35 " attributes\twhitespace-separated list of attributes to retrieve\n"
36 "\t\t1.1 -- no attributes\n"
37 "\t\t* -- all user attributes\n"
38 "\t\t+ -- all operational attributes\n"
39 "\t\tempty list -- all non-operational attributes\n"
41 " -a deref\tone of `never', `always', `search', or `find' (alias\n"
43 " -A\t\tretrieve attribute names only (no values)\n"
44 " -b basedn\tbase dn for search\n"
45 " -B\t\tdo not suppress printing of binary values\n"
46 " -d level\tset LDAP debugging level to `level'\n"
47 " -D binddn\tbind DN\n"
48 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
49 " -f file\t\tperform sequence of searches listed in `file'\n"
50 " -F sep\t\tprint `sep' instead of `=' between attribute names and\n"
52 " -h host\t\tLDAP server\n"
53 " -I\t\trequest SASL integrity checking (-II to make it\n"
55 " -k\t\tuse Kerberos authentication\n"
56 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
57 " -l limit\ttime limit (in seconds) for search\n"
58 " -L\t\tprint entries in LDIF format (implies -B)\n"
59 " -LL\t\tprint entries in LDIF format without comments\n"
60 " -LLL\t\tprint entries in LDIF format without comments and\n"
62 " -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
63 " -n\t\tshow what would be done but don't actually search\n"
64 " -p port\t\tport on LDAP server\n"
65 " -P version\tprocotol version (2 or 3)\n"
66 " -R\t\tdo not automatically follow referrals\n"
67 " -s scope\tone of base, one, or sub (search scope)\n"
68 " -S attr\t\tsort the results by attribute `attr'\n"
69 " -t\t\twrite binary values to files in TMPDIR\n"
70 " -tt\t\twrite all values to files in TMPDIR\n"
71 " -T path\t\twrite files to directory specified by path (default:\n"
73 " -u\t\tinclude User Friendly entry names in the output\n"
74 " -U user\t\tSASL authentication identity (username)\n"
75 " -v\t\trun in verbose mode (diagnostics to standard output)\n"
76 " -V prefix\tURL prefix for files (default: \"file://tmp/\")\n"
77 " -w passwd\tbind passwd (for simple authentication)\n"
78 " -W\t\tprompt for bind passwd\n"
79 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
80 " -Y mech\t\tSASL mechanism\n"
81 " -z limit\tsize limit (in entries) for search\n"
82 " -Z\t\trequest the use of TLS (-ZZ to make it critical)\n"
88 static void print_entry LDAP_P((
93 static int write_ldif LDAP_P((
99 static int dosearch LDAP_P((
108 #define TMPDIR "/tmp"
109 #define URLPRE "file:/tmp/"
111 static char *tmpdir = NULL;
112 static char *urlpre = NULL;
114 static char *binddn = NULL;
115 static char *passwd = NULL;
116 static char *base = NULL;
117 static char *ldaphost = NULL;
118 static int ldapport = 0;
119 #ifdef HAVE_CYRUS_SASL
120 static char *sasl_authc_id = NULL;
121 static char *sasl_authz_id = NULL;
122 static char *sasl_mech = NULL;
123 static int sasl_integrity = 0;
124 static int sasl_privacy = 0;
126 static int use_tls = 0;
127 static char *sep = DEFSEP;
128 static char *sortattr = NULL;
129 static int skipsortattr = 0;
130 static int verbose, not, includeufn, binary, vals2tmp, ldif;
133 main( int argc, char **argv )
135 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
137 int rc, i, first, scope, deref, attrsonly, manageDSAit;
138 int referrals, timelimit, sizelimit, debug;
139 int authmethod, version, want_bindpw;
143 debug = verbose = binary = not = vals2tmp =
144 attrsonly = manageDSAit = ldif = want_bindpw = 0;
146 deref = referrals = sizelimit = timelimit = version = -1;
148 scope = LDAP_SCOPE_SUBTREE;
149 authmethod = LDAP_AUTH_SIMPLE;
151 while (( i = getopt( argc, argv,
152 "Aa:Bb:D:d:EF:f:h:IKkLl:MnP:p:RS:s:T:tU:uV:vWw:X:Y:Zz:")) != EOF )
155 case 'n': /* do Not do any searches */
158 case 'v': /* verbose mode */
162 debug |= atoi( optarg );
164 case 'k': /* use kerberos bind */
165 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
166 authmethod = LDAP_AUTH_KRBV4;
168 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
169 return( EXIT_FAILURE );
172 case 'K': /* use kerberos bind, 1st part only */
173 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
174 authmethod = LDAP_AUTH_KRBV41;
176 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
177 return( EXIT_FAILURE );
181 case 'u': /* include UFN */
184 case 't': /* write attribute values to /tmp files */
188 /* enable Manage DSA IT */
191 case 'R': /* don't automatically chase referrals */
192 referrals = (int) LDAP_OPT_OFF;
194 case 'A': /* retrieve attribute names only -- no values */
197 case 'L': /* print entries in LDIF format */
199 /* fall through -- always allow binary when outputting LDIF */
200 case 'B': /* allow binary values to be printed */
203 case 's': /* search scope */
204 if ( strcasecmp( optarg, "base" ) == 0 ) {
205 scope = LDAP_SCOPE_BASE;
206 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
207 scope = LDAP_SCOPE_ONELEVEL;
208 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
209 scope = LDAP_SCOPE_SUBTREE;
211 fprintf( stderr, "scope should be base, one, or sub\n" );
216 case 'a': /* set alias deref option */
217 if ( strcasecmp( optarg, "never" ) == 0 ) {
218 deref = LDAP_DEREF_NEVER;
219 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
220 deref = LDAP_DEREF_SEARCHING;
221 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
222 deref = LDAP_DEREF_FINDING;
223 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
224 deref = LDAP_DEREF_ALWAYS;
226 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
231 case 'T': /* field separator */
232 if( tmpdir ) free( tmpdir );
233 tmpdir = strdup( optarg );
235 case 'V': /* field separator */
236 if( urlpre ) free( urlpre );
237 urlpre = strdup( optarg );
239 case 'F': /* field separator */
240 sep = strdup( optarg );
242 case 'f': /* input file */
243 infile = strdup( optarg );
245 case 'h': /* ldap host */
246 ldaphost = strdup( optarg );
248 case 'b': /* searchbase */
249 base = strdup( optarg );
251 case 'D': /* bind DN */
252 binddn = strdup( optarg );
254 case 'p': /* ldap port */
255 ldapport = atoi( optarg );
257 case 'w': /* bind password */
258 passwd = strdup( optarg );
262 for( p = optarg; *p == '\0'; p++ ) {
267 case 'l': /* time limit */
268 timelimit = atoi( optarg );
270 case 'z': /* size limit */
271 sizelimit = atoi( optarg );
273 case 'S': /* sort attribute */
274 sortattr = strdup( optarg );
280 switch( atoi( optarg ) )
283 version = LDAP_VERSION2;
286 version = LDAP_VERSION3;
289 fprintf( stderr, "protocol version should be 2 or 3\n" );
294 #ifdef HAVE_CYRUS_SASL
296 authmethod = LDAP_AUTH_SASL;
298 fprintf( stderr, "%s was not compiled with SASL support\n",
300 return( EXIT_FAILURE );
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
315 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
316 sasl_mech = strdup( optarg );
318 authmethod = LDAP_AUTH_SASL;
320 fprintf( stderr, "%s was not compiled with SASL support\n",
322 return( EXIT_FAILURE );
326 #ifdef HAVE_CYRUS_SASL
327 sasl_authc_id = 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_authz_id = strdup( optarg );
338 authmethod = LDAP_AUTH_SASL;
340 fprintf( stderr, "%s was not compiled with SASL support\n",
342 return( EXIT_FAILURE );
349 fprintf( stderr, "%s was not compiled with TLS support\n",
351 return( EXIT_FAILURE );
360 /* no alternative format */
361 if( ldif < 1 ) ldif = 1;
364 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
365 LDAP_AUTH_KRBV41 ) ) {
366 if( version > LDAP_VERSION2 ) {
367 fprintf( stderr, "Kerberos requires LDAPv2\n" );
368 return( EXIT_FAILURE );
370 version = LDAP_VERSION2;
372 else if ( authmethod == LDAP_AUTH_SASL ) {
373 if( version != -1 || version != LDAP_VERSION3 ) {
374 fprintf( stderr, "SASL requires LDAPv3\n" );
375 return( EXIT_FAILURE );
377 version = LDAP_VERSION3;
381 if( version != -1 || version != LDAP_VERSION3 ) {
382 fprintf(stderr, "manage DSA control requires LDAPv3\n");
385 version = LDAP_VERSION3;
389 if( version != -1 || version != LDAP_VERSION3 ) {
390 fprintf(stderr, "Start TLS requires LDAPv3\n");
393 version = LDAP_VERSION3;
396 if ( argc - optind < 1 ) {
400 filtpattern = strdup( argv[ optind ] );
402 if ( argv[ optind + 1 ] == NULL ) {
404 } else if ( sortattr == NULL || *sortattr == '\0' ) {
405 attrs = &argv[ optind + 1 ];
407 for ( i = optind + 1; i < argc; i++ ) {
408 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
414 argv[ optind ] = sortattr;
418 attrs = &argv[ optind ];
421 if ( infile != NULL ) {
422 if ( infile[0] == '-' && infile[1] == '\0' ) {
424 } else if (( fp = fopen( infile, "r" )) == NULL ) {
431 && (tmpdir = getenv("TMPDIR")) == NULL
432 && (tmpdir = getenv("TMP")) == NULL
433 && (tmpdir = getenv("TEMP")) == NULL )
438 if( urlpre == NULL ) {
439 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
441 if( urlpre == NULL ) {
446 sprintf( urlpre, "file:///%s/",
447 tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
449 /* urlpre should be URLized.... */
453 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
454 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
456 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
457 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
463 (void) SIGNAL( SIGPIPE, SIG_IGN );
468 (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
469 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
473 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
474 perror( "ldap_init" );
475 return( EXIT_FAILURE );
479 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
481 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
483 if (timelimit != -1 &&
484 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
486 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
488 if (sizelimit != -1 &&
489 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
491 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
493 if (referrals != -1 &&
494 ldap_set_option( ld, LDAP_OPT_REFERRALS,
495 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
497 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
498 referrals ? "on" : "off" );
502 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
504 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
507 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
509 ldap_perror( ld, "ldap_start_tls" );
510 return( EXIT_FAILURE );
515 passwd = getpass("Enter LDAP Password: ");
518 if ( authmethod == LDAP_AUTH_SASL ) {
519 #ifdef HAVE_CYRUS_SASL
520 int minssf = 0, maxssf = 0;
522 if ( sasl_integrity > 0 )
524 if ( sasl_integrity > 1 )
526 if ( sasl_privacy > 0 )
527 maxssf = 100000; /* Something big value */
528 if ( sasl_privacy > 1 )
531 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
532 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
533 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
535 return( EXIT_FAILURE );
537 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
538 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
539 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
541 return( EXIT_FAILURE );
544 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
545 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
547 ldap_perror( ld, "ldap_sasl_bind" );
548 return( EXIT_FAILURE );
551 fprintf( stderr, "%s was not compiled with SASL support\n",
553 return( EXIT_FAILURE );
557 if ( ldap_bind_s( ld, binddn, passwd, authmethod )
559 ldap_perror( ld, "ldap_bind" );
560 return( EXIT_FAILURE );
567 LDAPControl *ctrls[2];
571 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
572 c.ldctl_value.bv_val = NULL;
573 c.ldctl_value.bv_len = 0;
574 c.ldctl_iscritical = manageDSAit > 1;
576 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
578 if( err != LDAP_OPT_SUCCESS ) {
579 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
580 if( c.ldctl_iscritical ) {
581 exit( EXIT_FAILURE );
587 fprintf( stderr, "filter%s: %s\nreturning: ",
588 infile != NULL ? " pattern" : "",
591 if ( attrs == NULL ) {
592 fprintf( stderr, "ALL" );
594 for ( i = 0; attrs[ i ] != NULL; ++i ) {
595 fprintf( stderr, "%s ", attrs[ i ] );
598 fprintf( stderr, "\n" );
603 printf( "version: 1\n\n");
607 printf( "#\n# filter%s: %s\n# returning: ",
608 infile != NULL ? " pattern" : "",
611 if ( attrs == NULL ) {
614 for ( i = 0; attrs[ i ] != NULL; ++i ) {
615 printf( "%s ", attrs[ i ] );
622 if ( infile == NULL ) {
623 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
628 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
629 line[ strlen( line ) - 1 ] = '\0';
635 rc = dosearch( ld, base, scope, attrs, attrsonly,
657 char filter[ BUFSIZ ];
658 int rc, first, matches;
659 LDAPMessage *res, *e;
661 if( filtpatt != NULL ) {
662 sprintf( filter, filtpatt, value );
665 fprintf( stderr, "filter is: (%s)\n", filter );
669 printf( "#\n# filter: %s\n#\n", filter );
673 sprintf( filter, "%s", value );
677 return( LDAP_SUCCESS );
680 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
682 ldap_perror( ld, "ldap_search" );
684 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
691 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
692 == LDAP_RES_SEARCH_ENTRY ) {
694 e = ldap_first_entry( ld, res );
700 print_entry( ld, e, attrsonly );
705 ldap_perror( ld, "ldap_result" );
708 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
709 ldap_perror( ld, "ldap_search" );
711 if ( sortattr != NULL ) {
712 (void) ldap_sort_entries( ld, &res,
713 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
716 for ( e = ldap_first_entry( ld, res ); e != NULL;
717 e = ldap_next_entry( ld, e ) ) {
724 print_entry( ld, e, attrsonly );
729 printf( "%d matches\n", matches );
744 char tmpfname[ 256 ];
747 BerElement *ber = NULL;
748 struct berval **bvals;
751 dn = ldap_get_dn( ld, entry );
755 ufn = ldap_dn2ufn( dn );
756 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
759 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
761 printf( "%s\n", dn );
766 ufn = ldap_dn2ufn( dn );
769 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
771 printf( "%s\n", ufn );
775 if( ufn != NULL ) ldap_memfree( ufn );
778 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
779 a = ldap_next_attribute( ld, entry, ber ) )
781 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
787 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
792 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
793 for ( i = 0; bvals[i] != NULL; i++ ) {
794 if ( vals2tmp > 1 || ( vals2tmp
795 && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
797 /* write value to file */
798 sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
802 if ( mktemp( tmpfname ) == NULL ) {
807 if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
812 if ( fwrite( bvals[ i ]->bv_val,
813 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
822 sprintf( url, "%s%s", urlpre,
823 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
826 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
828 printf( "%s%s%s\n", a, sep, url );
834 write_ldif( LDIF_PUT_VALUE, a,
835 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
838 int notprint = !binary && !vals2tmp
839 && ldif_is_not_printable( bvals[i]->bv_val,
841 printf( "%s%s", a, sep );
842 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
846 ber_bvecfree( bvals );
857 write_ldif( int type, char *name, char *value, ber_len_t vallen )
861 if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
865 fputs( ldif, stdout );